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