Subversion Repositories HelenOS

Rev

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

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