Subversion Repositories HelenOS

Rev

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

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