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