Rev 323 | Rev 353 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 323 | Rev 352 | ||
---|---|---|---|
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 paging_on |
37 | .global paging_on |
38 | .global enable_l_apic_in_msr |
38 | .global enable_l_apic_in_msr |
39 | .global interrupt_handlers |
39 | .global interrupt_handlers |
40 | .global inb |
40 | .global inb |
41 | .global inw |
41 | .global inw |
42 | .global inl |
42 | .global inl |
43 | .global outb |
- | |
44 | .global outw |
43 | .global outw |
45 | .global outl |
44 | .global outl |
46 | .global memcpy |
45 | .global memcpy |
47 | .global memsetb |
46 | .global memsetb |
48 | .global memsetw |
47 | .global memsetw |
49 | .global memcmp |
48 | .global memcmp |
50 | 49 | ||
51 | 50 | ||
52 | ## Turn paging on |
51 | ## Turn paging on |
53 | # |
52 | # |
54 | # Enable paging and write-back caching in CR0. |
53 | # Enable paging and write-back caching in CR0. |
55 | # |
54 | # |
56 | paging_on: |
55 | paging_on: |
57 | movl %cr0,%edx |
56 | movl %cr0,%edx |
58 | orl $(1<<31),%edx # paging on |
57 | orl $(1<<31),%edx # paging on |
59 | andl $~((1<<30)|(1<<29)),%edx # clear Cache Disable and not Write Though |
58 | andl $~((1<<30)|(1<<29)),%edx # clear Cache Disable and not Write Though |
60 | movl %edx,%cr0 |
59 | movl %edx,%cr0 |
61 | jmp 0f |
60 | jmp 0f |
62 | 0: |
61 | 0: |
63 | ret |
62 | ret |
64 | 63 | ||
65 | 64 | ||
66 | ## Enable local APIC |
65 | ## Enable local APIC |
67 | # |
66 | # |
68 | # Enable local APIC in MSR. |
67 | # Enable local APIC in MSR. |
69 | # |
68 | # |
70 | enable_l_apic_in_msr: |
69 | enable_l_apic_in_msr: |
71 | push %eax |
70 | push %eax |
72 | 71 | ||
73 | movl $0x1b, %ecx |
72 | movl $0x1b, %ecx |
74 | rdmsr |
73 | rdmsr |
75 | orl $(1<<11),%eax |
74 | orl $(1<<11),%eax |
76 | orl $(0xfee00000),%eax |
75 | orl $(0xfee00000),%eax |
77 | wrmsr |
76 | wrmsr |
78 | 77 | ||
79 | pop %eax |
78 | pop %eax |
80 | ret |
79 | ret |
81 | 80 | ||
82 | 81 | ||
83 | ## Declare interrupt handlers |
82 | ## Declare interrupt handlers |
84 | # |
83 | # |
85 | # Declare interrupt handlers for n interrupt |
84 | # Declare interrupt handlers for n interrupt |
86 | # vectors starting at vector i. |
85 | # vectors starting at vector i. |
87 | # |
86 | # |
88 | # The handlers setup data segment registers |
87 | # The handlers setup data segment registers |
89 | # and call trap_dispatcher(). |
88 | # and call trap_dispatcher(). |
90 | # |
89 | # |
91 | .macro handler i n |
90 | .macro handler i n |
92 | push %ebp |
91 | push %ebp |
93 | movl %esp,%ebp |
92 | movl %esp,%ebp |
94 | pusha |
93 | pusha |
95 | 94 | ||
96 | push %ds |
95 | push %ds |
97 | push %es |
96 | push %es |
98 | 97 | ||
99 | # we must fill the data segment registers |
98 | # we must fill the data segment registers |
100 | movw $16,%ax |
99 | movw $16,%ax |
101 | movw %ax,%ds |
100 | movw %ax,%ds |
102 | movw %ax,%es |
101 | movw %ax,%es |
103 | 102 | ||
104 | movl $(\i),%edi |
103 | movl $(\i),%edi |
105 | pushl %ebp |
104 | pushl %ebp |
106 | addl $4,(%esp) |
105 | addl $4,(%esp) |
107 | pushl %edi |
106 | pushl %edi |
108 | call trap_dispatcher |
107 | call trap_dispatcher |
109 | addl $8,%esp |
108 | addl $8,%esp |
110 | 109 | ||
111 | pop %es |
110 | pop %es |
112 | pop %ds |
111 | pop %ds |
113 | 112 | ||
114 | 113 | ||
115 | # CLNT |
114 | # CLNT |
116 | pushfl |
115 | pushfl |
117 | pop %eax |
116 | pop %eax |
118 | and $0xFFFFBFFF,%eax |
117 | and $0xFFFFBFFF,%eax |
119 | push %eax |
118 | push %eax |
120 | popfl |
119 | popfl |
121 | 120 | ||
122 | 121 | ||
123 | 122 | ||
124 | # Test if this is interrupt with error word or not |
123 | # Test if this is interrupt with error word or not |
125 | mov $\i,%cl |
124 | mov $\i,%cl |
126 | movl $1,%eax |
125 | movl $1,%eax |
127 | test $0xe0,%cl |
126 | test $0xe0,%cl |
128 | jnz 0f |
127 | jnz 0f |
129 | and $0x1f,%cl |
128 | and $0x1f,%cl |
130 | shl %cl,%eax |
129 | shl %cl,%eax |
131 | and $ERROR_WORD_INTERRUPT_LIST,%eax |
130 | and $ERROR_WORD_INTERRUPT_LIST,%eax |
132 | jz 0f |
131 | jz 0f |
133 | 132 | ||
134 | 133 | ||
135 | # Return with error word |
134 | # Return with error word |
136 | popa |
135 | popa |
137 | pop %ebp |
136 | pop %ebp |
138 | add $4,%esp # Skip error word |
137 | add $4,%esp # Skip error word |
139 | iret |
138 | iret |
140 | 139 | ||
141 | 0: |
140 | 0: |
142 | # Return with no error word |
141 | # Return with no error word |
143 | popa |
142 | popa |
144 | pop %ebp |
143 | pop %ebp |
145 | iret |
144 | iret |
146 | 145 | ||
147 | .if (\n-\i)-1 |
146 | .if (\n-\i)-1 |
148 | handler "(\i+1)",\n |
147 | handler "(\i+1)",\n |
149 | .endif |
148 | .endif |
150 | .endm |
149 | .endm |
151 | 150 | ||
152 | # keep in sync with pm.h !!! |
151 | # keep in sync with pm.h !!! |
153 | IDT_ITEMS=64 |
152 | IDT_ITEMS=64 |
154 | interrupt_handlers: |
153 | interrupt_handlers: |
155 | h_start: |
154 | h_start: |
156 | handler 0 64 |
155 | handler 0 64 |
157 | # handler 64 128 |
156 | # handler 64 128 |
158 | # handler 128 192 |
157 | # handler 128 192 |
159 | # handler 192 256 |
158 | # handler 192 256 |
160 | h_end: |
159 | h_end: |
161 | 160 | ||
162 | 161 | ||
163 | ## I/O input (byte) |
162 | ## I/O input (byte) |
164 | # |
163 | # |
165 | # Get a byte from I/O port and store it AL. |
164 | # Get a byte from I/O port and store it AL. |
166 | # |
165 | # |
167 | inb: |
166 | inb: |
168 | xorl %eax,%eax |
167 | xorl %eax,%eax |
169 | movl 4(%esp),%edx |
168 | movl 4(%esp),%edx |
170 | inb %dx,%al |
169 | inb %dx,%al |
171 | ret |
170 | ret |
172 | 171 | ||
173 | 172 | ||
174 | ## I/O input (word) |
173 | ## I/O input (word) |
175 | # |
174 | # |
176 | # Get a word from I/O port and store it AX. |
175 | # Get a word from I/O port and store it AX. |
177 | # |
176 | # |
178 | inw: |
177 | inw: |
179 | xorl %eax,%eax |
178 | xorl %eax,%eax |
180 | movl 4(%esp),%edx |
179 | movl 4(%esp),%edx |
181 | inw %dx,%ax |
180 | inw %dx,%ax |
182 | ret |
181 | ret |
183 | 182 | ||
184 | 183 | ||
185 | ## I/O input (dword) |
184 | ## I/O input (dword) |
186 | # |
185 | # |
187 | # Get a dword from I/O port and store it EAX. |
186 | # Get a dword from I/O port and store it EAX. |
188 | # |
187 | # |
189 | inl: |
188 | inl: |
190 | xorl %eax,%eax |
189 | xorl %eax,%eax |
191 | movl 4(%esp),%edx |
190 | movl 4(%esp),%edx |
192 | inl %dx,%eax |
191 | inl %dx,%eax |
193 | ret |
192 | ret |
194 | - | ||
195 | - | ||
196 | ## I/O output (byte) |
- | |
197 | # |
- | |
198 | # Send a byte to I/O port. |
- | |
199 | # |
- | |
200 | outb: |
- | |
201 | push %eax |
- | |
202 | - | ||
203 | movl 8(%esp),%edx |
- | |
204 | movl 12(%esp),%eax |
- | |
205 | outb %al,%dx |
- | |
206 | - | ||
207 | pop %eax |
- | |
208 | ret |
- | |
209 | 193 | ||
210 | 194 | ||
211 | ## I/O output (word) |
195 | ## I/O output (word) |
212 | # |
196 | # |
213 | # Send a word to I/O port. |
197 | # Send a word to I/O port. |
214 | # |
198 | # |
215 | outw: |
199 | outw: |
216 | push %eax |
200 | push %eax |
217 | 201 | ||
218 | movl 8(%esp),%edx |
202 | movl 8(%esp),%edx |
219 | movl 12(%esp),%eax |
203 | movl 12(%esp),%eax |
220 | outw %ax,%dx |
204 | outw %ax,%dx |
221 | 205 | ||
222 | pop %eax |
206 | pop %eax |
223 | ret |
207 | ret |
224 | 208 | ||
225 | 209 | ||
226 | ## I/O output (dword) |
210 | ## I/O output (dword) |
227 | # |
211 | # |
228 | # Send a dword to I/O port. |
212 | # Send a dword to I/O port. |
229 | # |
213 | # |
230 | outl: |
214 | outl: |
231 | push %eax |
215 | push %eax |
232 | 216 | ||
233 | movl 8(%esp),%edx |
217 | movl 8(%esp),%edx |
234 | movl 12(%esp),%eax |
218 | movl 12(%esp),%eax |
235 | outl %eax,%dx |
219 | outl %eax,%dx |
236 | 220 | ||
237 | pop %eax |
221 | pop %eax |
238 | ret |
222 | ret |
239 | 223 | ||
240 | 224 | ||
241 | ## Copy memory |
225 | ## Copy memory |
242 | # |
226 | # |
243 | # Copy a given number of bytes (3rd argument) |
227 | # Copy a given number of bytes (3rd argument) |
244 | # from the memory location defined by 2nd argument |
228 | # from the memory location defined by 2nd argument |
245 | # to the memory location defined by 1st argument. |
229 | # to the memory location defined by 1st argument. |
246 | # The memory areas cannot overlap. |
230 | # The memory areas cannot overlap. |
247 | # |
231 | # |
248 | SRC=16 |
232 | SRC=16 |
249 | DST=12 |
233 | DST=12 |
250 | CNT=20 |
234 | CNT=20 |
251 | memcpy: |
235 | memcpy: |
252 | push %esi |
236 | push %esi |
253 | push %edi |
237 | push %edi |
254 | 238 | ||
255 | movl CNT(%esp),%ecx |
239 | movl CNT(%esp),%ecx |
256 | movl DST(%esp),%edi |
240 | movl DST(%esp),%edi |
257 | movl SRC(%esp),%esi |
241 | movl SRC(%esp),%esi |
258 | 242 | ||
259 | rep movsb %ds:(%esi),%es:(%edi) |
243 | rep movsb %ds:(%esi),%es:(%edi) |
260 | 244 | ||
261 | pop %edi |
245 | pop %edi |
262 | pop %esi |
246 | pop %esi |
263 | ret |
247 | ret |
264 | 248 | ||
265 | 249 | ||
266 | ## Fill memory with bytes |
250 | ## Fill memory with bytes |
267 | # |
251 | # |
268 | # Fill a given number of bytes (2nd argument) |
252 | # Fill a given number of bytes (2nd argument) |
269 | # at memory defined by 1st argument with the |
253 | # at memory defined by 1st argument with the |
270 | # byte value defined by 3rd argument. |
254 | # byte value defined by 3rd argument. |
271 | # |
255 | # |
272 | DST=12 |
256 | DST=12 |
273 | CNT=16 |
257 | CNT=16 |
274 | X=20 |
258 | X=20 |
275 | memsetb: |
259 | memsetb: |
276 | push %eax |
260 | push %eax |
277 | push %edi |
261 | push %edi |
278 | 262 | ||
279 | movl CNT(%esp),%ecx |
263 | movl CNT(%esp),%ecx |
280 | movl DST(%esp),%edi |
264 | movl DST(%esp),%edi |
281 | movl X(%esp),%eax |
265 | movl X(%esp),%eax |
282 | 266 | ||
283 | rep stosb %al,%es:(%edi) |
267 | rep stosb %al,%es:(%edi) |
284 | 268 | ||
285 | pop %edi |
269 | pop %edi |
286 | pop %eax |
270 | pop %eax |
287 | ret |
271 | ret |
288 | 272 | ||
289 | 273 | ||
290 | ## Fill memory with words |
274 | ## Fill memory with words |
291 | # |
275 | # |
292 | # Fill a given number of words (2nd argument) |
276 | # Fill a given number of words (2nd argument) |
293 | # at memory defined by 1st argument with the |
277 | # at memory defined by 1st argument with the |
294 | # word value defined by 3rd argument. |
278 | # word value defined by 3rd argument. |
295 | # |
279 | # |
296 | DST=12 |
280 | DST=12 |
297 | CNT=16 |
281 | CNT=16 |
298 | X=20 |
282 | X=20 |
299 | memsetw: |
283 | memsetw: |
300 | push %eax |
284 | push %eax |
301 | push %edi |
285 | push %edi |
302 | 286 | ||
303 | movl CNT(%esp),%ecx |
287 | movl CNT(%esp),%ecx |
304 | movl DST(%esp),%edi |
288 | movl DST(%esp),%edi |
305 | movl X(%esp),%eax |
289 | movl X(%esp),%eax |
306 | 290 | ||
307 | rep stosw %ax,%es:(%edi) |
291 | rep stosw %ax,%es:(%edi) |
308 | 292 | ||
309 | pop %edi |
293 | pop %edi |
310 | pop %eax |
294 | pop %eax |
311 | 295 | ||
312 | ret |
296 | ret |
313 | 297 | ||
314 | 298 | ||
315 | ## Compare memory regions for equality |
299 | ## Compare memory regions for equality |
316 | # |
300 | # |
317 | # Compare a given number of bytes (3rd argument) |
301 | # Compare a given number of bytes (3rd argument) |
318 | # at memory locations defined by 1st and 2nd argument |
302 | # at memory locations defined by 1st and 2nd argument |
319 | # for equality. If the bytes are equal, EAX contains |
303 | # for equality. If the bytes are equal, EAX contains |
320 | # 0. |
304 | # 0. |
321 | # |
305 | # |
322 | SRC=12 |
306 | SRC=12 |
323 | DST=16 |
307 | DST=16 |
324 | CNT=20 |
308 | CNT=20 |
325 | memcmp: |
309 | memcmp: |
326 | push %esi |
310 | push %esi |
327 | push %edi |
311 | push %edi |
328 | 312 | ||
329 | movl CNT(%esp),%ecx |
313 | movl CNT(%esp),%ecx |
330 | movl DST(%esp),%edi |
314 | movl DST(%esp),%edi |
331 | movl SRC(%esp),%esi |
315 | movl SRC(%esp),%esi |
332 | 316 | ||
333 | repe cmpsb %es:(%edi),%ds:(%esi) |
317 | repe cmpsb %es:(%edi),%ds:(%esi) |
334 | movl %ecx,%eax # %ecx contains the return value (zero on success) |
318 | movl %ecx,%eax # %ecx contains the return value (zero on success) |
335 | 319 | ||
336 | pop %edi |
320 | pop %edi |
337 | pop %esi |
321 | pop %esi |
338 | 322 | ||
339 | ret |
323 | ret |
340 | 324 | ||
341 | 325 | ||
342 | # THIS IS USERSPACE CODE |
326 | # THIS IS USERSPACE CODE |
343 | .global utext |
327 | .global utext |
344 | utext: |
328 | utext: |
345 | xor %ax,%ax |
329 | xor %ax,%ax |
346 | mov %ax,%ds |
330 | mov %ax,%ds |
347 | mov %ax,%es |
331 | mov %ax,%es |
348 | mov %ax,%fs |
332 | mov %ax,%fs |
349 | mov %ax,%gs |
333 | mov %ax,%gs |
350 | 0: |
334 | 0: |
351 | int $48 |
335 | int $48 |
352 | jmp 0b |
336 | jmp 0b |
353 | # not reached |
337 | # not reached |
354 | utext_end: |
338 | utext_end: |
355 | 339 | ||
356 | .data |
340 | .data |
357 | .global utext_size |
341 | .global utext_size |
358 | utext_size: |
342 | utext_size: |
359 | .long utext_end - utext |
343 | .long utext_end - utext |
360 | 344 | ||
361 | .global interrupt_handler_size |
345 | .global interrupt_handler_size |
362 | 346 | ||
363 | interrupt_handler_size: .long (h_end-h_start)/IDT_ITEMS |
347 | interrupt_handler_size: .long (h_end-h_start)/IDT_ITEMS |
364 | 348 |