Subversion Repositories HelenOS-historic

Rev

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

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