Page 1 of 1

Copying does not work in unreal mode

Posted: Wed Jul 19, 2006 11:48 am
by jason7007
Hi,

I 'd been trying to copy my Kernel from linear 0x20000 to 0x100000
(1 mb mark). I already setup Unreal mode, enabled line A20, but to no avail.

This code is the second stage boot loader that was loaded at
0x1000:0x0000 by the first stage boot loader.

Can someone point to me what I'd been doing wrong here, Please.

Here is the code:

%define OS_LOADER_LINEAR 0x10000

[ORG 0]
BITS 16]

JMP START

GdtUnreal:
dw GdtUnreal_end - GdtUnreal_start - 1 ;Size of table
dd GdtUnreal_start + OS_LOADER_LINEAR

GdtUnreal_start:
dw 0, 0, 0, 0 ;Null descriptor, 4 words all set to '0'
dw 0FFFFh ;Part of limit
dw 0 ;Base address 15:00, is 0
db 0 ;Base address 23:16, is 0
db 92h ;Present, ring 0, data, expand-up, writable
db 0CFh?????? ;Granular, 32-bit
db 0
GdtUnreal_end:

START:
CLI
PUSH CS
POP AX
MOV DS, AX
MOV ES, AX
XOR AX, AX
MOV SS, AX
MOV SP,0FFFFH

CALL ENABLE_A20

;Setup for Unreal mode

CLI
LGDT [GdtUnreal]
MOV EAX, CR0
OR AL, 1
MOV CR0, EAX???
???
; leave real-mode cs, ss in place -- just set ds and es
MOV AX, 08H
MOV DS, AX
MOV ES, AX
; back to (un)real mode (big real mode, flat real mode, whatever).
MOV EAX, CR0
AND AL, 0xFE???
MOV CR0, EAX
STI
???
;INITIALLY THE KERNEL WAS LOADED TO 0x2000:0x0000 BY ;THIS SECOND STAGE LOADER???

;COPY THE KERNEL TO 1 MB MARK

???XOR EAX, EAX
???XOR EBX, EBX
???XOR ECX, ECX
???XOR EDI, EDI
???XOR ESI, ESI
???
???
???MOV ES, AX ;SOURCE SEGMENT
???MOV DS, AX ;DESTINATION SEGMENT
???
???MOV ESI, 0x20000 ; SOURCE OFFSET
???MOV EDI, 0x100000 ;DESTINATION OFFSET??????
???MOV ECX, 65536 ;QUANTITY OF BYTES TO COPY
???CLD
???DB 0x67???
???REP MOVSB ;COPY BYTE FROM DS:[ESI] TO ES:[EDI]
RET




ENABLE_A20:
??? CLI
??? XOR CX,CX
.IBEmm0:
IN AL,64H
TEST AL,02
??? LOOPNZ .IBEmm0
MOV AL,0D1H
OUT 64H,AL
??? XOR CX,CX
.IBEmm1:
IN AL,64H
TEST AL,02
??? LOOPNZ .IBEmm1
MOV AL,0DFH
OUT 60H,AL
??? XOR CX,CX
.IBEmm2:
IN AL,64H
TEST AL,02
??? LOOPNZ .IBEmm2
STI
RET

Re:Copying does not work in unreal mode

Posted: Wed Jul 19, 2006 12:51 pm
by FlashBurn
The problem is that you use "movsb" it only uses di and si also in unreal mode. You have to use "mov al,[ds:esi]" and "mov [ds:edi],al" with ds set to 0x00000000! This is not only faster, but is the only way that I know at the moment to copy the data behind the 1MiB mark!

Re:Copying does not work in unreal mode

Posted: Wed Jul 19, 2006 2:25 pm
by Ryu
Try keeping interrupts disabled from the start. I'm also iffy' if your enabling A20 routine if it actually works, because you use 0:FFFFh stack pointer (which is better aligned in words btw), if the A20 is not enabled this will corrupt the stack.

Re:Copying does not work in unreal mode

Posted: Wed Jul 19, 2006 6:11 pm
by jason7007
Hi,

Flashburn , I tried your method, It freezed my screen.

I already use checking A20 line if it is enable, yes it is enabled, but still this code is not working.


Any help please.

Thank you anyway.

Re:Copying does not work in unreal mode

Posted: Wed Jul 19, 2006 10:22 pm
by Ryu
Well, if you could supply us some sort of error information such as from Bochs and if you know the offending instruction, it will help us rather then guessing. I really don't see anything wrong here if A20 line is enabled, but you should keep interrupts disabled as well, unless you have special interrupt handlers for both real mode and unreal modes. I'm also not sure why you have a ret instruction after rep movsb, which is a problem. Run this in Bochs and replace that ret with cli and hlt, and check boschout.txt if theres any errors and registers. If there is, clue us in as well.

Re:Copying does not work in unreal mode

Posted: Wed Jul 19, 2006 11:34 pm
by Cody
FlashBurn wrote: The problem is that you use "movsb" it only uses di and si also in unreal mode. You have to use "mov al,[ds:esi]" and "mov [ds:edi],al" with ds set to 0x00000000! This is not only faster, but is the only way that I know at the moment to copy the data behind the 1MiB mark!
No, movsb here is correct. The source code has DB67 prefix before the rep movsb which will change the default operand size from 16 bit to 32 bit and ESI, EDI will be used.

Re:Copying does not work in unreal mode

Posted: Thu Jul 20, 2006 12:39 am
by FlashBurn
I don?t know how you used my code, but a memcpy for unreal mode should look like this (maybe I should have said that you need to inc the edi and esi reg):

Code: Select all

;esi=source
;edi=destination
;ecx=count
memcpy:
xor ax,ax
xor ebx,ebx
mov ds,ax
.loop:
mov al,[ds:esi+ebx]
mov [ds:edi+ebx]
add ebx,1
sub ecx,1
jnz .loop
ret

Re:Copying does not work in unreal mode

Posted: Thu Jul 20, 2006 2:49 am
by Ryu
Cody wrote: No, movsb here is correct. The source code has DB67 prefix before the rep movsb which will change the default operand size from 16 bit to 32 bit and ESI, EDI will be used.
More accurately, changes the address size, 66h is you operand override prefix. :)

Re:Copying does not work in unreal mode

Posted: Thu Jul 20, 2006 5:56 pm
by Cody
Ryu wrote:
Cody wrote: No, movsb here is correct. The source code has DB67 prefix before the rep movsb which will change the default operand size from 16 bit to 32 bit and ESI, EDI will be used.
More accurately, changes the address size, 66h is you operand override prefix. :)
Yep, ;) you are right. Any idea on the problem here?

Best regards,
Cody

Re:Copying does not work in unreal mode

Posted: Thu Jul 20, 2006 8:33 pm
by jason7007
Hi,

I discovered the above code that I posted works on my Development computer but not on Testing computer and Bochs.

Development Computer is
Compaq Despro
Pentium III

Testing Computer is
Asus Motherboard
Pentium II


Any hint why?

Thank you.

Re:Copying does not work in unreal mode

Posted: Thu Jul 20, 2006 10:39 pm
by Candy
That's a pretty direct hint your A20 line is not being enabled on bochs + test computer. Newer computers have it off by default (thank god), but of course not all new computers....

Re:Copying does not work in unreal mode

Posted: Fri Jul 21, 2006 12:10 am
by Cody
Candy wrote: That's a pretty direct hint your A20 line is not being enabled on bochs + test computer. Newer computers have it off by default (thank god), but of course not all new computers....
Hi, Jason

In that way, how about your trying to activate A20M# using Port 92? You could have a try by adding the following line in your code right after your EnableA20 call:

in al, 0x92
And al, 0xFD
out 0x92, al

Best regards,
Cody

Re:Copying does not work in unreal mode

Posted: Sat Jul 22, 2006 5:02 pm
by jason7007
Hi,

I already solved this problem, in my real loader the value of
0x100000, which is the destination address is assigned to the variable DESTINATION declared like this

DESTINATION DD 0.

My final COPY_DATA code now is like this

;COPY THE KERNEL TO 1 MB MARK

XOR EAX, EAX
XOR EBX, EBX
XOR ECX, ECX
XOR EDI, EDI
XOR ESI, ESI




MOV ESI, 0x20000 ; SOURCE OFFSET
MOV EDI, [DESTINATION] ESTINATION OFFSET
MOV ECX, 65536 ;QUANTITY OF BYTES TO COPY
CLD
PUSH DS
PUSH ES
MOV ES, AX ;SOURCE SEGMENT
MOV DS, AX ESTINATION SEGMENT
DB 0x67
REP MOVSB ;COPY BYTE FROM DS:[ESI] TO ES:[EDI]
POP ES
POP DS

Notice: I just changed the value of DS and ES just before the
code DB 0x67. This is to avoid changing the real mode address
of all variables.


Thanks to all who responded.

Re:Copying does not work in unreal mode

Posted: Sun Jul 23, 2006 6:47 pm
by Cody
Notice: I just changed the value of DS and ES just before the
code DB 0x67. This is to avoid changing the real mode address
of all variables.
I didn't see any real difference between this version and your original one. And I don't think reserving your DS, ES before data copying can have any effects. It may help on other things anyway.

Best regards,
Cody