Hello!
I am in a state of confusion. That program enters protected mode,
initializes segment registers, loads TR, jumps on the second task. Then
second task is trying to jump on the first one -- and the program
receives the general-protection exception.
What am I doing wrong?
I load the program in a emulator such as qemu or bochs.
Here is following GAS listing:
1 .code16
2 _start:
3 0000 E93301 jmp _init
4 0003 908DB400 .align 8
4 00
5 0008 00000000 .space 0x20 - 8
5 00000000
5 00000000
5 00000000
5 00000000
6 GDT:
7 0020 00000000 .long 0, 0 # empty descriptor
7 00000000
8 ## limit | base | PPLStype |
GDL limit| base
9 0028 FFFF007C .byte 0xFF, 0xFF, 0x00, 0x7C, 0x00, 0b10011010,
0b11001111, 0x00 # code
9 009ACF00
10 0030 FFFF007C .byte 0xFF, 0xFF, 0x00, 0x7C, 0x00, 0b10010010,
0b11001111, 0x00 # data
10 0092CF00
11 0038 FFFF0080 .byte 0xFF, 0xFF, 0x00, 0x80, 0x0B, 0b10010010,
0b11001111, 0x00 # video buffer
11 0B92CF00
12 0040 FFFF007C .byte 0xFF, 0xFF, 0x00, 0x7C, 0x00, 0b10011010,
0b10001111, 0x00 # real mode code
12 009A8F00
13 0048 5000007C .byte 0x50, 0x00, 0x00, 0x7C, 0x00, 0b10010010,
0b01000000, 0x00 # stack
13 00924000
14
15 0050 6700 .byte 0x67, 0x00
16 0052 667C .word TSS1 + 0x7C00
17 0054 00894000 .byte 0x00, 0b10001001, 0b01000000, 0x00 # tss1
18
19 0058 6700 .byte 0x67, 0x00
20 005a CE7C .word TSS2 + 0x7C00
21 005c 00894000 .byte 0x00, 0b10001001, 0b01000000, 0x00 # tss2
22
23 .set _CS32, 0b1000
24 .set _DS32, 0b10000
25 .set _ES32, 0b11000
26 .set _CS16, 0b100000
27 .set _SS32, 0b101000
28 .set _TSS1, 0b110000
29 .set _TSS2, 0b111000
30 GDT_end:
31 .set GDT_size, GDT_end - GDT
32 GDTR:
33 0060 3F00 .word GDT_size - 1
34 0062 207C0000 .long GDT + 0x7C00
35 TSS1:
36 0066 00000000 .space 0x68, 0
36 00000000
36 00000000
36 00000000
36 00000000
37 TSS2:
38 00ce 00000000 .space 4 * 8, 0
38 00000000
38 00000000
38 00000000
38 00000000
39 00ee AC020000 .long _task2 # EIP
40 00f2 00000000 .space 4 * 5, 0
40 00000000
40 00000000
40 00000000
40 00000000
41 0106 15000000 .long 0x15, 0, 0, 2 # ESP EDI
41 00000000
41 00000000
41 02000000
42 0116 18000000 .long _ES32, _CS32, _SS32, _DS32, 0, 0
42 08000000
42 28000000
42 10000000
42 00000000
43 012e 00000000 .long 0, 0
43 00000000
44 _init:
45 ## clear the screen
46 0136 B80300 movw $3, %ax
47 0139 CD10 int $0x10
48
49 ## initialization of real mode segments
50 013b B8C007 movw $0x7C0, %ax
51 013e 8ED8 movw %ax, %ds
52 0140 8EC0 movw %ax, %es
53
54 ## reading of program's rest
55 0142 B402 movb $2, %ah
56 0144 B005 movb $5, %al
57 0146 30ED xorb %ch, %ch
58 0148 B102 movb $2, %cl
59 014a 31D2 xorw %dx, %dx
60 014c BB0002 movw $_continue_loading, %bx
61 014f CD13 int $0x13
62 0151 7211 jc _display_read_err
63 0153 E9AA00 jmp _continue_loading
64
65 _read_err:
66 0156 4572726F .ascii "Error occured!"
66 72206F63
66 63757265
66 6421
67 _read_err_end:
68 .set _read_err_len, _read_err_end - _read_err
69
70 _display_read_err:
71 0164 BD5601 movw $_read_err, %bp
72 0167 B90E00 movw $_read_err_len, %cx
73 016a 31D2 xorw %dx, %dx
74 016c BB0700 movw $0x0007, %bx
75 016f B80113 movw $0x1301, %ax
76 0172 CD10 int $0x10
77
78 0174 EBFE jmp .
79
80 .align 2
81 0176 55AA55AA .balignw 512, 0xAA55
81 55AA55AA
81 55AA55AA
81 55AA55AA
81 55AA55AA
82
83 _continue_loading:
84 0200 EB21 jmp _succ
85 _read_succ:
86 0202 52656164 .ascii "Reading of sectors is successful!"
86 696E6720
86 6F662073
86 6563746F
86 72732069
87 _read_succ_end:
88 .set _read_succ_len, _read_succ_end - _read_succ
89 _succ:
90 0223 BD0202 movw $_read_succ, %bp
91 0226 B92100 movw $_read_succ_len, %cx
92 0229 BA0001 movw $0x0100, %dx
93 022c BB0700 movw $0x0007, %bx
94 022f B80113 movw $0x1301, %ax
95 0232 CD10 int $0x10
96
97 ## open address line
98 0234 E492 inb $0x92, %al
99 0236 0C02 orb $2, %al
100 0238 E692 outb %al, $0x92
101
102 ## deny all interupts
103 023a FA cli
104 023b E470 inb $0x70, %al
105 023d 0C80 orb $0x80, %al
106 023f E670 outb %al, $0x70 # NMI
107
108 0241 0F011660 lgdt GDTR
108 00
109
110 ## switching to protected mode
111 0246 0F20C0 movl %cr0, %eax
112 0249 0C01 orb $1, %al
113 024b 0F22C0 movl %eax, %cr0
114
115 ## loading new selector to cs
116 024e EA530208 jmp $_CS32, $_protected
116 00
117
118 .code32
119 _protected:
120 0253 EB1E jmp _disp
121 _switch_pm:
122 0255 53077707
.byte 'S',7,'w',7,'i',7,'t',7,'c',7,'h',7,'e',7,'d',7,'
',7,'t',7,'o',7,' ',7,'P',7,'M',7,'!',7
122 69077407
122 63076807
122 65076407
122 20077407
123 _switch_pm_end:
124 .set _switch_pm_len, _switch_pm_end - _switch_pm
125 _disp:
126 ## initialization of protected mode selectors
127 0273 66B81000 movw $_DS32, %ax
128 0277 8ED8 movw %ax, %ds
129 0279 66B81800 movw $_ES32, %ax
130 027d 8EC0 movw %ax, %es
131 027f 66B82800 movw $_SS32, %ax
132 0283 8ED0 movw %ax, %ss
133 0285 BC100000 movl $0x10, %esp
133 00
134
135 028a 66B83000 movw $_TSS1, %ax
136 028e 0F00D8 ltr %ax
137
138 ## allowing hardware interrupts
139 0291 E470 inb $0x70, %al
140 0293 247F andb $0x7F, %al
141 0295 E670 outb %al, $0x70
142 0297 FB sti
143
144 0298 31FF xorl %edi, %edi
145 _task1:
146 029a 2666C707 movw $0x4741, %es:(%edi)
146 4147
147 02a0 FEC0 incb %al
148 02a2 83C702 addl $2, %edi
149 02a5 EA000000 jmp $_TSS2, $0
149 003800
150 _task2:
151 02ac 2666C707 movw $0x4742, %es:(%edi)
151 4247
152 02b2 FEC0 incb %al
153 02b4 83C702 addl $2, %edi
154 02b7 B9FFFFFF movl $0xffffff, %ecx
154 00
155 02bc E2FE loop .
156 02be EA000000 jmp $_TSS1, $0
156 003000
157 _exit_to_rm:
158 ## deny all interupts
159 02c5 FA cli
160 02c6 E470 inb $0x70, %al
161 02c8 0C80 orb $0x80, %al
162 02ca E670 outb %al, $0x70 # NMI
163
164 02cc EAD30200 jmp $_CS16, $_real
164 002000
165
166 .code16
167 _real:
168 02d3 0F20C0 movl %cr0, %eax
169 02d6 24FE andb $0xFE, %al
170 02d8 0F22C0 movl %eax, %cr0
171
172 02db EAE002C0 jmp $0x7C0, $_real_entry
172 07
173
174 _real_entry:
175 02e0 B8C007 movw $0x7C0, %ax
176 02e3 8ED8 movw %ax, %ds
177 02e5 8EC0 movw %ax, %es
178 02e7 31C0 xorw %ax, %ax
179 02e9 8ED0 movw %ax, %ss
180 02eb BCFEFF movw $0xfffe, %sp
181
182 02ee EB0F jmp _disp_rm
183 _switch_rm:
184 02f0 53776974 .ascii "Switched to RM!"
184 63686564
184 20746F20
184 524D21
185 _switch_rm_end:
186 .set _switch_rm_len, _switch_rm_end - _switch_rm
187 _disp_rm:
188
189 02ff BDF002 movw $_switch_rm, %bp
190 0302 B90F00 movw $_switch_rm_len, %cx
191 0305 BA0003 movw $0x0300, %dx
192 0308 BB0700 movw $0x0007, %bx
193 030b B80113 movw $0x1301, %ax
194 030e CD10 int $0x10
195
196 ## allowing hardware interrupts
197 0310 E470 inb $0x70, %al
198 0312 247F andb $0x7F, %al
199 0314 E670 outb %al, $0x70
200 0316 FB sti
201
202 0317 EBFE jmp .
203
204 0319 90909090 .align 512
204 90909090
204 90909090
204 90909090
204 90909090


|