Rev 136 | Rev 141 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
132 | vana | 1 | # |
2 | # Copyright (C) 2001-2004 Jakub Jermar |
||
3 | # All rights reserved. |
||
4 | # |
||
5 | # Redistribution and use in source and binary forms, with or without |
||
6 | # modification, are permitted provided that the following conditions |
||
7 | # are met: |
||
8 | # |
||
9 | # - Redistributions of source code must retain the above copyright |
||
10 | # notice, this list of conditions and the following disclaimer. |
||
11 | # - Redistributions in binary form must reproduce the above copyright |
||
12 | # notice, this list of conditions and the following disclaimer in the |
||
13 | # documentation and/or other materials provided with the distribution. |
||
14 | # - The name of the author may not be used to endorse or promote products |
||
15 | # derived from this software without specific prior written permission. |
||
16 | # |
||
17 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
18 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
19 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
22 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
23 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
24 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
25 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
26 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | # |
||
28 | |||
29 | ## very low and hardware-level functions |
||
30 | |||
137 | vana | 31 | # Mask for interrupts 0 - 31 (bits 0 - 31) where 0 means that int has no error word |
32 | # and 1 means interrupt with error word |
||
132 | vana | 33 | #define ERROR_WORD_INTERRUPT_LIST 0x00027D00 |
34 | |||
35 | .text |
||
36 | |||
37 | .global cpu_halt |
||
38 | .global cpu_sleep |
||
39 | .global paging_on |
||
40 | .global enable_l_apic_in_msr |
||
41 | .global interrupt_handlers |
||
42 | .global inb |
||
43 | .global inw |
||
44 | .global inl |
||
45 | .global outb |
||
46 | .global outw |
||
47 | .global outl |
||
48 | .global memcopy |
||
49 | .global memsetb |
||
50 | .global memsetw |
||
51 | .global memcmp |
||
52 | |||
53 | |||
54 | ## Turn paging on |
||
55 | # |
||
56 | # Enable paging and write-back caching in CR0. |
||
57 | # |
||
58 | paging_on: |
||
59 | pushl %eax |
||
60 | movl %cr0,%eax |
||
61 | orl $(1<<31),%eax # paging on |
||
62 | andl $~((1<<30)|(1<<29)),%eax # clear Cache Disable and not Write Though |
||
63 | movl %eax,%cr0 |
||
64 | jmp 0f |
||
65 | 0: |
||
66 | popl %eax |
||
67 | ret |
||
68 | |||
69 | |||
70 | ## Enable local APIC |
||
71 | # |
||
72 | # Enable local APIC in MSR. |
||
73 | # |
||
74 | enable_l_apic_in_msr: |
||
75 | pusha |
||
76 | |||
77 | movl $0x1b, %ecx |
||
78 | rdmsr |
||
79 | orl $(1<<11),%eax |
||
80 | orl $(0xfee00000),%eax |
||
81 | wrmsr |
||
82 | |||
83 | popa |
||
84 | ret |
||
85 | |||
86 | |||
87 | ## Declare interrupt handlers |
||
88 | # |
||
89 | # Declare interrupt handlers for n interrupt |
||
90 | # vectors starting at vector i. |
||
91 | # |
||
92 | # The handlers setup data segment registers |
||
93 | # and call trap_dispatcher(). |
||
94 | # |
||
95 | .macro handler i n |
||
96 | push %ebp |
||
97 | movl %esp,%ebp |
||
98 | pusha |
||
99 | |||
100 | push %ds |
||
101 | push %es |
||
102 | |||
103 | # we must fill the data segment registers |
||
104 | movw $16,%ax |
||
105 | movw %ax,%ds |
||
106 | movw %ax,%es |
||
107 | |||
108 | movl $(\i),%edi |
||
109 | pushl %ebp |
||
110 | addl $4,(%esp) |
||
111 | pushl %edi |
||
112 | call trap_dispatcher |
||
113 | addl $8,%esp |
||
114 | |||
115 | pop %es |
||
116 | pop %ds |
||
117 | |||
137 | vana | 118 | |
119 | # Test if this is interrupt with error word or not |
||
132 | vana | 120 | mov $\i,%cl; |
121 | movl $1,%eax; |
||
122 | test $0xe0,%cl; |
||
123 | jnz 0f; |
||
136 | vana | 124 | and $0x1f,%cl; |
132 | vana | 125 | shl %cl,%eax; |
126 | and $ERROR_WORD_INTERRUPT_LIST,%eax; |
||
136 | vana | 127 | jz 0f; |
132 | vana | 128 | |
137 | vana | 129 | |
130 | # Return with error word |
||
132 | vana | 131 | popa; |
132 | pop %ebp; |
||
137 | vana | 133 | add $4,%esp; # Skip error word |
132 | vana | 134 | iret; |
135 | |||
136 | 0: |
||
137 | vana | 137 | # Return with no error word |
132 | vana | 138 | popa |
139 | pop %ebp |
||
140 | iret |
||
141 | |||
142 | .if (\n-\i)-1 |
||
143 | handler "(\i+1)",\n |
||
144 | .endif |
||
145 | .endm |
||
146 | |||
147 | # keep in sync with pm.h !!! |
||
148 | IDT_ITEMS=64 |
||
149 | interrupt_handlers: |
||
150 | h_start: |
||
151 | handler 0 64 |
||
152 | # handler 64 128 |
||
153 | # handler 128 192 |
||
154 | # handler 192 256 |
||
155 | h_end: |
||
156 | |||
157 | |||
158 | ## I/O input (byte) |
||
159 | # |
||
160 | # Get a byte from I/O port and store it AL. |
||
161 | # |
||
162 | inb: |
||
163 | push %edx |
||
164 | xorl %eax,%eax |
||
165 | movl 8(%esp),%edx |
||
166 | inb %dx,%al |
||
167 | pop %edx |
||
168 | ret |
||
169 | |||
170 | |||
171 | ## I/O input (word) |
||
172 | # |
||
173 | # Get a word from I/O port and store it AX. |
||
174 | # |
||
175 | inw: |
||
176 | push %edx |
||
177 | xorl %eax,%eax |
||
178 | movl 8(%esp),%edx |
||
179 | inw %dx,%ax |
||
180 | pop %edx |
||
181 | ret |
||
182 | |||
183 | |||
184 | ## I/O input (dword) |
||
185 | # |
||
186 | # Get a dword from I/O port and store it EAX. |
||
187 | # |
||
188 | inl: |
||
189 | push %edx |
||
190 | xorl %eax,%eax |
||
191 | movl 8(%esp),%edx |
||
192 | inl %dx,%eax |
||
193 | pop %edx |
||
194 | ret |
||
195 | |||
196 | |||
197 | ## I/O output (byte) |
||
198 | # |
||
199 | # Send a byte to I/O port. |
||
200 | # |
||
201 | outb: |
||
202 | push %ebp |
||
203 | movl %esp,%ebp |
||
204 | pusha |
||
205 | |||
206 | movl 8(%ebp),%edx |
||
207 | movl 12(%ebp),%eax |
||
208 | outb %al,%dx |
||
209 | |||
210 | popa |
||
211 | pop %ebp |
||
212 | ret |
||
213 | |||
214 | |||
215 | ## I/O output (word) |
||
216 | # |
||
217 | # Send a word to I/O port. |
||
218 | # |
||
219 | outw: |
||
220 | push %ebp |
||
221 | movl %esp,%ebp |
||
222 | pusha |
||
223 | |||
224 | movl 8(%ebp),%edx |
||
225 | movl 12(%ebp),%eax |
||
226 | outw %ax,%dx |
||
227 | |||
228 | popa |
||
229 | pop %ebp |
||
230 | ret |
||
231 | |||
232 | |||
233 | ## I/O output (dword) |
||
234 | # |
||
235 | # Send a dword to I/O port. |
||
236 | # |
||
237 | outl: |
||
238 | push %ebp |
||
239 | movl %esp,%ebp |
||
240 | pusha |
||
241 | |||
242 | movl 8(%ebp),%edx |
||
243 | movl 12(%ebp),%eax |
||
244 | outl %eax,%dx |
||
245 | |||
246 | popa |
||
247 | pop %ebp |
||
248 | ret |
||
249 | |||
250 | |||
251 | ## Copy memory |
||
252 | # |
||
253 | # Copy a given number of bytes (3rd argument) |
||
254 | # from the memory location defined by 1st argument |
||
255 | # to the memory location defined by 2nd argument. |
||
256 | # The memory areas cannot overlap. |
||
257 | # |
||
258 | SRC=8 |
||
259 | DST=12 |
||
260 | CNT=16 |
||
261 | memcopy: |
||
262 | push %ebp |
||
263 | movl %esp,%ebp |
||
264 | pusha |
||
265 | |||
266 | cld |
||
267 | movl CNT(%ebp),%ecx |
||
268 | movl DST(%ebp),%edi |
||
269 | movl SRC(%ebp),%esi |
||
270 | |||
271 | rep movsb %ds:(%esi),%es:(%edi) |
||
272 | |||
273 | popa |
||
274 | pop %ebp |
||
275 | ret |
||
276 | |||
277 | |||
278 | ## Fill memory with bytes |
||
279 | # |
||
280 | # Fill a given number of bytes (2nd argument) |
||
281 | # at memory defined by 1st argument with the |
||
282 | # byte value defined by 3rd argument. |
||
283 | # |
||
284 | DST=8 |
||
285 | CNT=12 |
||
286 | X=16 |
||
287 | memsetb: |
||
288 | push %ebp |
||
289 | movl %esp,%ebp |
||
290 | pusha |
||
291 | |||
292 | cld |
||
293 | movl CNT(%ebp),%ecx |
||
294 | movl DST(%ebp),%edi |
||
295 | movl X(%ebp),%eax |
||
296 | |||
297 | rep stosb %al,%es:(%edi) |
||
298 | |||
299 | popa |
||
300 | pop %ebp |
||
301 | ret |
||
302 | |||
303 | |||
304 | ## Fill memory with words |
||
305 | # |
||
306 | # Fill a given number of words (2nd argument) |
||
307 | # at memory defined by 1st argument with the |
||
308 | # word value defined by 3rd argument. |
||
309 | # |
||
310 | DST=8 |
||
311 | CNT=12 |
||
312 | X=16 |
||
313 | memsetw: |
||
314 | push %ebp |
||
315 | movl %esp,%ebp |
||
316 | pusha |
||
317 | |||
318 | cld |
||
319 | movl CNT(%ebp),%ecx |
||
320 | movl DST(%ebp),%edi |
||
321 | movl X(%ebp),%eax |
||
322 | |||
323 | rep stosw %ax,%es:(%edi) |
||
324 | |||
325 | popa |
||
326 | pop %ebp |
||
327 | ret |
||
328 | |||
329 | |||
330 | ## Compare memory regions for equality |
||
331 | # |
||
332 | # Compare a given number of bytes (3rd argument) |
||
333 | # at memory locations defined by 1st and 2nd argument |
||
334 | # for equality. If the bytes are equal, EAX contains |
||
335 | # 0. |
||
336 | # |
||
337 | SRC=12 |
||
338 | DST=16 |
||
339 | CNT=20 |
||
340 | memcmp: |
||
341 | push %ebp |
||
342 | subl $4,%esp |
||
343 | movl %esp,%ebp |
||
344 | |||
345 | pusha |
||
346 | |||
347 | cld |
||
348 | movl CNT(%ebp),%ecx |
||
349 | movl DST(%ebp),%edi |
||
350 | movl SRC(%ebp),%esi |
||
351 | |||
352 | repe cmpsb %es:(%edi),%ds:(%esi) |
||
353 | movl %ecx,(%ebp) |
||
354 | |||
355 | popa |
||
356 | |||
357 | movl (%ebp),%eax # return value => %eax (zero on success) |
||
358 | addl $4,%esp |
||
359 | pop %ebp |
||
360 | |||
361 | ret |
||
362 | |||
363 | |||
364 | # THIS IS USERSPACE CODE |
||
365 | .global utext |
||
366 | utext: |
||
136 | vana | 367 | xor %ax,%ax; |
368 | mov %ax,%ds; |
||
369 | mov %ax,%es; |
||
370 | mov %ax,%fs; |
||
371 | mov %ax,%gs; |
||
132 | vana | 372 | 0: |
373 | # movl $0xdeadbeaf, %eax |
||
136 | vana | 374 | int $48 |
375 | cli; |
||
376 | sti; |
||
132 | vana | 377 | jmp 0b |
378 | # not reached |
||
379 | utext_end: |
||
380 | |||
381 | .data |
||
382 | .global utext_size |
||
383 | utext_size: |
||
384 | .long utext_end - utext |
||
385 | |||
386 | |||
387 | #.section K_DATA_START |
||
388 | .global interrupt_handler_size |
||
389 | |||
390 | interrupt_handler_size: .long (h_end-h_start)/IDT_ITEMS |