Subversion Repositories HelenOS-historic

Rev

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

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