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