Rev 356 | Rev 358 | Go to most recent revision | Details | Compare with Previous | 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 | |||
137 | vana | 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 |
||
132 | vana | 33 | #define ERROR_WORD_INTERRUPT_LIST 0x00027D00 |
34 | |||
35 | .text |
||
36 | |||
37 | .global paging_on |
||
38 | .global enable_l_apic_in_msr |
||
39 | .global interrupt_handlers |
||
205 | jermar | 40 | .global memcpy |
132 | vana | 41 | .global memsetb |
42 | .global memsetw |
||
43 | .global memcmp |
||
44 | |||
45 | |||
46 | ## Turn paging on |
||
47 | # |
||
48 | # Enable paging and write-back caching in CR0. |
||
49 | # |
||
50 | paging_on: |
||
316 | jermar | 51 | movl %cr0,%edx |
52 | orl $(1<<31),%edx # paging on |
||
53 | andl $~((1<<30)|(1<<29)),%edx # clear Cache Disable and not Write Though |
||
54 | movl %edx,%cr0 |
||
132 | vana | 55 | jmp 0f |
56 | 0: |
||
57 | ret |
||
58 | |||
59 | |||
60 | ## Enable local APIC |
||
61 | # |
||
62 | # Enable local APIC in MSR. |
||
63 | # |
||
64 | enable_l_apic_in_msr: |
||
316 | jermar | 65 | push %eax |
66 | |||
132 | vana | 67 | movl $0x1b, %ecx |
68 | rdmsr |
||
69 | orl $(1<<11),%eax |
||
70 | orl $(0xfee00000),%eax |
||
71 | wrmsr |
||
316 | jermar | 72 | |
73 | pop %eax |
||
132 | vana | 74 | ret |
75 | |||
76 | |||
77 | ## Declare interrupt handlers |
||
78 | # |
||
79 | # Declare interrupt handlers for n interrupt |
||
80 | # vectors starting at vector i. |
||
81 | # |
||
82 | # The handlers setup data segment registers |
||
83 | # and call trap_dispatcher(). |
||
84 | # |
||
85 | .macro handler i n |
||
86 | push %ebp |
||
87 | movl %esp,%ebp |
||
88 | pusha |
||
89 | |||
90 | push %ds |
||
91 | push %es |
||
92 | |||
93 | # we must fill the data segment registers |
||
94 | movw $16,%ax |
||
95 | movw %ax,%ds |
||
96 | movw %ax,%es |
||
97 | |||
98 | movl $(\i),%edi |
||
99 | pushl %ebp |
||
100 | addl $4,(%esp) |
||
101 | pushl %edi |
||
102 | call trap_dispatcher |
||
103 | addl $8,%esp |
||
104 | |||
105 | pop %es |
||
106 | pop %ds |
||
107 | |||
137 | vana | 108 | |
141 | vana | 109 | # CLNT |
316 | jermar | 110 | pushfl |
111 | pop %eax |
||
112 | and $0xFFFFBFFF,%eax |
||
113 | push %eax |
||
114 | popfl |
||
141 | vana | 115 | |
116 | |||
117 | |||
137 | vana | 118 | # Test if this is interrupt with error word or not |
316 | jermar | 119 | mov $\i,%cl |
120 | movl $1,%eax |
||
121 | test $0xe0,%cl |
||
122 | jnz 0f |
||
123 | and $0x1f,%cl |
||
124 | shl %cl,%eax |
||
125 | and $ERROR_WORD_INTERRUPT_LIST,%eax |
||
126 | jz 0f |
||
132 | vana | 127 | |
137 | vana | 128 | |
129 | # Return with error word |
||
316 | jermar | 130 | popa |
131 | pop %ebp |
||
132 | add $4,%esp # Skip error word |
||
133 | iret |
||
132 | vana | 134 | |
135 | 0: |
||
137 | vana | 136 | # Return with no error word |
132 | vana | 137 | popa |
138 | pop %ebp |
||
139 | iret |
||
140 | |||
141 | .if (\n-\i)-1 |
||
142 | handler "(\i+1)",\n |
||
143 | .endif |
||
144 | .endm |
||
145 | |||
146 | # keep in sync with pm.h !!! |
||
147 | IDT_ITEMS=64 |
||
148 | interrupt_handlers: |
||
149 | h_start: |
||
150 | handler 0 64 |
||
151 | # handler 64 128 |
||
152 | # handler 128 192 |
||
153 | # handler 192 256 |
||
154 | h_end: |
||
155 | |||
156 | |||
157 | ## Copy memory |
||
158 | # |
||
159 | # Copy a given number of bytes (3rd argument) |
||
198 | jermar | 160 | # from the memory location defined by 2nd argument |
161 | # to the memory location defined by 1st argument. |
||
132 | vana | 162 | # The memory areas cannot overlap. |
163 | # |
||
316 | jermar | 164 | SRC=16 |
165 | DST=12 |
||
166 | CNT=20 |
||
205 | jermar | 167 | memcpy: |
316 | jermar | 168 | push %esi |
169 | push %edi |
||
132 | vana | 170 | |
316 | jermar | 171 | movl CNT(%esp),%ecx |
172 | movl DST(%esp),%edi |
||
173 | movl SRC(%esp),%esi |
||
132 | vana | 174 | |
175 | rep movsb %ds:(%esi),%es:(%edi) |
||
176 | |||
316 | jermar | 177 | pop %edi |
178 | pop %esi |
||
132 | vana | 179 | ret |
180 | |||
181 | |||
182 | ## Fill memory with bytes |
||
183 | # |
||
184 | # Fill a given number of bytes (2nd argument) |
||
185 | # at memory defined by 1st argument with the |
||
186 | # byte value defined by 3rd argument. |
||
187 | # |
||
316 | jermar | 188 | DST=12 |
189 | CNT=16 |
||
190 | X=20 |
||
132 | vana | 191 | memsetb: |
316 | jermar | 192 | push %eax |
193 | push %edi |
||
132 | vana | 194 | |
316 | jermar | 195 | movl CNT(%esp),%ecx |
196 | movl DST(%esp),%edi |
||
197 | movl X(%esp),%eax |
||
132 | vana | 198 | |
199 | rep stosb %al,%es:(%edi) |
||
200 | |||
316 | jermar | 201 | pop %edi |
202 | pop %eax |
||
132 | vana | 203 | ret |
204 | |||
205 | |||
206 | ## Fill memory with words |
||
207 | # |
||
208 | # Fill a given number of words (2nd argument) |
||
209 | # at memory defined by 1st argument with the |
||
210 | # word value defined by 3rd argument. |
||
211 | # |
||
316 | jermar | 212 | DST=12 |
213 | CNT=16 |
||
214 | X=20 |
||
132 | vana | 215 | memsetw: |
316 | jermar | 216 | push %eax |
217 | push %edi |
||
132 | vana | 218 | |
316 | jermar | 219 | movl CNT(%esp),%ecx |
220 | movl DST(%esp),%edi |
||
221 | movl X(%esp),%eax |
||
132 | vana | 222 | |
223 | rep stosw %ax,%es:(%edi) |
||
224 | |||
316 | jermar | 225 | pop %edi |
226 | pop %eax |
||
227 | |||
132 | vana | 228 | ret |
229 | |||
230 | |||
231 | ## Compare memory regions for equality |
||
232 | # |
||
233 | # Compare a given number of bytes (3rd argument) |
||
234 | # at memory locations defined by 1st and 2nd argument |
||
235 | # for equality. If the bytes are equal, EAX contains |
||
236 | # 0. |
||
237 | # |
||
238 | SRC=12 |
||
239 | DST=16 |
||
240 | CNT=20 |
||
241 | memcmp: |
||
316 | jermar | 242 | push %esi |
243 | push %edi |
||
132 | vana | 244 | |
316 | jermar | 245 | movl CNT(%esp),%ecx |
246 | movl DST(%esp),%edi |
||
247 | movl SRC(%esp),%esi |
||
132 | vana | 248 | |
249 | repe cmpsb %es:(%edi),%ds:(%esi) |
||
316 | jermar | 250 | movl %ecx,%eax # %ecx contains the return value (zero on success) |
132 | vana | 251 | |
316 | jermar | 252 | pop %edi |
253 | pop %esi |
||
132 | vana | 254 | |
255 | ret |
||
256 | |||
257 | |||
258 | # THIS IS USERSPACE CODE |
||
259 | .global utext |
||
260 | utext: |
||
316 | jermar | 261 | xor %ax,%ax |
262 | mov %ax,%ds |
||
263 | mov %ax,%es |
||
264 | mov %ax,%fs |
||
265 | mov %ax,%gs |
||
132 | vana | 266 | 0: |
168 | jermar | 267 | int $48 |
132 | vana | 268 | jmp 0b |
269 | # not reached |
||
270 | utext_end: |
||
271 | |||
272 | .data |
||
273 | .global utext_size |
||
274 | utext_size: |
||
275 | .long utext_end - utext |
||
276 | |||
277 | .global interrupt_handler_size |
||
278 | |||
279 | interrupt_handler_size: .long (h_end-h_start)/IDT_ITEMS |