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