Skip to content

Commit 9e915b7

Browse files
committed
[FREELDR] Adapt some assembly code for amd64 position relative code (reactos#7530)
CORE-19882
1 parent 201247c commit 9e915b7

File tree

1 file changed

+39
-32
lines changed
  • boot/freeldr/freeldr/arch/realmode

1 file changed

+39
-32
lines changed

boot/freeldr/freeldr/arch/realmode/amd64.S

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,29 @@ Startup:
2828
mov byte ptr ds:[BSS_BootDrive], dl
2929
mov byte ptr ds:[BSS_BootPartition], dh
3030

31-
/* Setup a real mode stack */
32-
mov sp, word ptr ds:[stack16]
33-
34-
/* Output first status */
35-
mov si, offset Msg_Starting
36-
call writestr
31+
/* Setup the stack */
32+
mov sp, STACK16ADDR
3733

3834
/* Enable A20 address line */
3935
call EnableA20
4036

37+
/* Relocate FreeLdr if necessary */
38+
call RelocateFreeLdr
39+
ljmp16 FREELDR_BASE / 16, .SegmentRelocationPoint
40+
.SegmentRelocationPoint:
41+
42+
/* Output first status */
43+
mov si, offset Msg_Starting
44+
call cs_writestr
45+
4146
/* Check the CPU */
4247
call CheckFor64BitSupport
4348
test al, al
4449
jnz .LongModeSupported
4550

4651
/* Output failure message */
4752
mov si, offset Msg_Unsupported
48-
call writestr
53+
call cs_writestr
4954

5055
/* Wait for a keypress */
5156
int HEX(16)
@@ -64,16 +69,13 @@ Msg_LongModeSupported:
6469
.LongModeSupported:
6570
/* Output status */
6671
mov si, offset Msg_LongModeSupported
67-
call writestr
68-
69-
/* Load the GDT */
70-
lgdt lXdtPrefix ds:[gdtptr]
72+
call cs_writestr
7173

7274
/* Build the startup page tables */
7375
call BuildPageTables
7476

7577
/* Store real mode entry point in shared memory */
76-
mov dword ptr ds:[BSS_RealModeEntry], offset RealModeEntryPoint
78+
mov dword ptr ds:[BSS_RealModeEntry], offset RealModeEntryPoint + FREELDR_BASE
7779

7880
/* Address the image with es segment */
7981
mov ax, FREELDR_PE_BASE / 16
@@ -88,15 +90,15 @@ Msg_LongModeSupported:
8890
add eax, FREELDR_PE_BASE
8991

9092
/* Save entry point */
91-
mov dword ptr ds:[LongModeEntryPoint], eax
93+
mov dword ptr cs:[LongModeEntryPoint], eax
9294

9395
/* Restore es */
9496
xor ax, ax
9597
mov es, ax
9698

9799
/* Output status */
98100
mov si, offset Msg_SwitchToLongMode
99-
call writestr
101+
call cs_writestr
100102

101103
jmp ExitToLongMode
102104

@@ -109,14 +111,15 @@ gdt:
109111
.word HEX(0000), HEX(0000), HEX(0000), HEX(0000) /* 08: */
110112
.word HEX(0000), HEX(0000), HEX(9800), HEX(0020) /* 10: long mode CS */
111113
.word HEX(FFFF), HEX(0000), HEX(F300), HEX(00CF) /* 18: long mode DS */
112-
.word HEX(FFFF), HEX(0000), HEX(9E00), HEX(0000) /* 20: 16-bit real mode CS */
114+
.word HEX(FFFF), HEX(0000), HEX(9B00), HEX(008F) /* 20: 16-bit flat CS (!) */
113115
.word HEX(FFFF), HEX(0000), HEX(9200), HEX(0000) /* 28: 16-bit real mode DS */
114116
.word HEX(FFFF), HEX(0000), HEX(9B00), HEX(00CF) /* 30: compat mode CS */
117+
gdt_end:
115118

116119
/* GDT table pointer */
117120
gdtptr:
118-
.word HEX(37) /* Limit */
119-
.long OFF(gdt) /* Base Address */
121+
.word HEX(37) /* Limit */
122+
.long OFF(gdt) + FREELDR_BASE /* Base Address */
120123

121124

122125
CheckFor64BitSupport:
@@ -134,7 +137,7 @@ CheckFor64BitSupport:
134137
jnz .CheckForPAE
135138

136139
mov si, offset .Msg_NoCpuidSupport
137-
call writestr
140+
call cs_writestr
138141
popad
139142
xor al, al
140143
ret
@@ -151,7 +154,7 @@ CheckFor64BitSupport:
151154
je .CheckForLongMode
152155

153156
mov si, offset .Msg_NoPAE
154-
call writestr
157+
call cs_writestr
155158
popad
156159
xor al, al
157160
ret
@@ -175,7 +178,7 @@ CheckFor64BitSupport:
175178

176179
.NoLongMode:
177180
mov si, offset .Msg_NoLongMode
178-
call writestr
181+
call cs_writestr
179182
popad
180183
xor al, al
181184
ret
@@ -265,7 +268,7 @@ RealModeEntryPoint:
265268
mov cr0, eax
266269

267270
/* Clear prefetch queue & correct CS */
268-
ljmp16 0, InRealMode
271+
ljmp16 FREELDR_BASE / 16, InRealMode
269272

270273
InRealMode:
271274

@@ -290,28 +293,31 @@ InRealMode:
290293
xor esp, esp
291294

292295
/* Restore real mode stack */
293-
mov sp, word ptr ds:[stack16]
296+
mov sp, word ptr cs:[stack16]
294297

295298
// sti /* These are ok now */
296299

297300
/* Do the callback, specified by bx */
298301
shl bx, 1
299-
call word ptr ds:CallbackTable[bx]
302+
call word ptr cs:CallbackTable[bx]
300303

301304
ExitToLongMode:
302305
/* Disable interrupts */
303306
cli
304307

305308
/* Set correct segment registers */
306-
xor ax,ax
307-
mov ds,ax
308-
mov es,ax
309-
mov fs,ax
310-
mov gs,ax
311-
mov ss,ax
309+
xor ax, ax
310+
mov ds, ax
311+
mov es, ax
312+
mov fs, ax
313+
mov gs, ax
314+
mov ss, ax
312315

313316
/* Save current stack pointer */
314-
mov word ptr ds:[stack16], sp
317+
mov word ptr cs:[stack16], sp
318+
319+
/* Load the GDT */
320+
lgdt lXdtPrefix cs:[gdtptr]
315321

316322
/* Set PAE and PGE: 10100000b */
317323
mov eax, cr4
@@ -335,7 +341,7 @@ ExitToLongMode:
335341
mov cr0, eax
336342

337343
/* Clear prefetch queue & correct CS */
338-
ljmp16 LMODE_CS, InLongMode
344+
ljmp16 LMODE_CS, InLongMode + FREELDR_BASE
339345
InLongMode:
340346
//DB 66h, 0B8h, 18h, 00h // mov ax, LMODE_DS
341347
//DB 66h, 8Eh, 0D8h // mov ds, ax
@@ -350,6 +356,7 @@ LongModeEntryPoint:
350356

351357
int HEX(16)
352358
jmp Reboot
359+
LongModeEntryPointEnd:
353360

354361
/* FNID_* functions */
355362
CallbackTable:
@@ -363,7 +370,7 @@ CallbackTable:
363370

364371
/* 16-bit stack pointer */
365372
stack16:
366-
.word STACK16ADDR
373+
.word 0
367374

368375

369376
#include "int386.inc"

0 commit comments

Comments
 (0)