Subversion Repositories HelenOS

Rev

Rev 106 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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