Page 1 of 1

Help me, I can't enter the protected mode

Posted: Fri Mar 26, 2004 7:19 pm
by nicesj
Below code is simple boot code
but it is not work on my vmware

when i set cr0 (PE) bit, it is crash
i was try to figure out it myself, but i can't.

Please help me, What i missed...

This code makes me crazy..... oh god...

Code: Select all

; Bootsector

%define BOOTSEG   0x07c0      ; 0x07c0:offset => 0x7c00 + offset
%define INITSS   0x1000
%define INITSP   0xffff
%define INITSEG 0x2000

[ORG 0]
[BITS 16]

; Our size of boot code is 512 bytes (0x0200)
; We will load second code to 0x09c0(0x07c0 + 0x0200)
; After this code we will load second code for testing protected mode

jmp entry   ; jump to our private boot code!!
nop      ; someone use this for compatiblity of assembler

; Starting point
; ===============================================
entry:
   cli         ; disable interrupts
   mov ax, INITSS
   mov ss, ax      
   mov sp, INITSP       


   cld         ; clear direction
   mov ax, INITSEG
   mov es, ax      ; ES == 0x2000
   xor di, di

   mov ax, BOOTSEG      
   mov ds, ax      ; DS == 0x07c0
   xor si, si

   mov cx, 0x0100      ; 256
   ; DS:SI => ES:DI
   ; BOOTSEG => INITSEG
   ; movsb == 1
   ; movsw == 2
   ; movsd == 4
   repz movsw      ; repz : do 'movsw' 256 times => 256 * 2 = 512

   jmp INITSEG:main

main:
   ; ok, we jumped from BOOT Segment
   ; so we have to set our ds to here
   mov ax, INITSEG
   mov ds, ax

; Tell me something :)
   mov si, bootmsg
   call print

; I will load second sector to 0x2000:0x0000
;   mov ax, 0x2000      ; will be the base address of codesel
;   mov es, ax      ; 
;   mov bx, 0x0      ; 
;   mov al, 1      ; 
;   mov cl, 2      ; 
;   call read_sector

; load Global Descriptor Table
   lgdt [gdt_ptr]
; enter the protected mode
   mov eax, cr0
   or al, 1      ; toggle PE bit
   mov cr0, eax

bits 32
   jmp codesel:pmode   ; now we have code descriptor and selector, so we will use it for far jump

pmode:
   jmp pmode


bits 16
; ----------------------------------------------------------
; Functions
; ==========================================================
print:
   cld   ; direction forward
   lodsb   ; next character   ; ds:si
   cmp al, 0
   jz p_done;
   mov ah, 0x0E
   mov bx, 0x0007
   int 0x10
   jmp print
p_done:
   ret

waitkey:
   mov ah,0
   int 0x16
   ret

read_sector:
;  es:bx = address of destination (memory)
;  al = sector count
;  cl = sector start number
   mov ah, 0x02
   mov ch, 0
   mov dl, [bootdrive]
   mov dh, 0
   int 0x13
   jc read_sector
   ret
; End of code section, I don't want to execute below data

; =====================
; Data section
; =====================
bootdrive   db   0
bootmsg      db   'SJOS v0.1', 13, 10, 0
pressmsg   db   'Press any key', 13, 10, 0

gdt_ptr
   dw end_gdt - begin_gdt - 1   ; limit
   dd begin_gdt         ; base address
begin_gdt
   dd   0x00000000   ; null descriptor
   dd   0x00000000   

; these two descriptors are indicate same data, becuase we will implement the flat model
codesel equ $-begin_gdt   ; start address of code descriptor(index), we will use this for selector(cs)
code0
   dw 0xffff   ; segment limit
   dw 0x0000   ; base address
   db 0x00
   db 0x9a      ; 10011010 - priv level 0, code
   db 0xcf      ; 11001111 - granurlarity
   db 0x00      ; 00000000 - base address
datasel equ $-begin_gdt   ; start address of data descriptor(index), we will use this for selector(ds)
data0
   dw 0xffff
   dw 0x0000
   db 0x00
   db 0x92
   db 0xcf
   db 0x00; Bootsector

Re:Help me, I can't enter the protected mode

Posted: Sat Mar 27, 2004 2:10 am
by Therx
I think the "bits 32" should come after the far jump.

HTH

Pete

Re:Help me, I can't enter the protected mode

Posted: Sat Mar 27, 2004 4:30 am
by nicesj
it makes different errors

vmware says,

Code: Select all

*** VMware Workstation internal monitor error ***
NOT_IMPLEMENTED at 4008c2 (6030)

Code: 004008c2-0000178e-acb8c78c-97bb8edc-a59986a3-ee89f6a1-9dd4b8d9-a8bd

Please report this problem by selecting menu item Help > VMware on the Web > Request Support, or by going to the Web page "http://www.vmware.com/info?id=8& sn=2V4RH%2d5YR8H%2d321F6%2d4K5V0&logFile=%2fmnt%2fdmz%2f win2000Pro%2fvmware%2elog&coreLocation=%2fmnt %2fdmz%2fwin2000Pro%2fvmware%2dcore%2egz". Please provide us with the log file (/mnt/dmz/win2000Pro/vmware.log) and the core file (/mnt/dmz/win2000Pro/vmware-core.gz).
If the problem is repeatable, please set 'Logging level' to 'Debug' in the Misc panel of the configuration editor. Then reproduce the incident and file it according to the instructions.
We will respond on the basis of your support entitlement.
We appreciate your feedback,
  -- the VMware Workstation team.
am i missed something?

i want to design for flat model..

???

Re:Help me, I can't enter the protected mode

Posted: Sat Mar 27, 2004 4:41 am
by DennisCGc
Uhm, if you are using NASM, use jmp dword codesel:pmode
I don't know sure if that's making any difference ::)
But I'm not sure ::)

Re:Help me, I can't enter the protected mode

Posted: Sun Mar 28, 2004 8:04 am
by aladdin
try to use these values

Code: Select all

%define INITSS   0x9000
%define INITSP   0x9F00

Re:Help me, I can't enter the protected mode

Posted: Sun Mar 28, 2004 10:25 am
by Schol-R-LEA
Is the code posted here complete? NASM v.98 gets two errors in assembling it as it is, both of which would be indicative of some code being missing. The actual errors are:

Code: Select all

E:\Joseph Osako\Programming\projects\sjpark pmode\boot.asm:120: error: symbol `end_gdt' undefined

E:\Joseph Osako\Programming\projects\sjpark pmode\boot.asm:142: error: phase error detected at end of assembly.
I am working with an unmodified copy of the posted code.

EDIT: When I add an 'end_gdt:' label followed by padding and the boot signature, it assembles fine; however, I don't know for certain if any other code is missing. I was able to determine that it does run correctly and that the print and waitkey functions do work despite interrupts being cleared, at least under Bochs v.2.0.2, so I should be able to help determine where the problem lies in the p-mode code once I have the missing code (if any).

Re:Help me, I can't enter the protected mode

Posted: Sun Mar 28, 2004 9:43 pm
by nicesj
oh..
i missed last 3 lines

Code: Select all

end_gdt
; ======================
;    End of GDT
; ======================

times   510 - ($ - $$)   db   0    ; fill rest area of boot sector with '0'
dw   0xAA55            ; Signature of Boot Sector
maybe these lines are not pasted correctly.

and i will paste modified code

Code: Select all

; Bootsector
; 20 bits address line?
; 16 bits segment selector size
; 16 bits < 4 = segment
; offset = 16 bits

[ORG 0]
[BITS 16]

%define BOOTSEG   0x07c0      ; 0x07c0:offset => 0x7c00 + offset
%define INITSS   0x9000      ; Stack Segment for real mode
%define INITSP   0x9f00      ; Stack Pointer for real mode
%define INITSEG 0x2000      ; Initial segment, we will run our code from here

; Our size of boot code is 512 bytes (0x0200)
; We will load second code to 0x09c0(0x07c0 + 0x0200)
; After this code we will load second code for testing protected mode

jmp entry   ; jump to our private boot code!!
nop      ; someone use this for compatiblity of assembler

; Starting point
; ===============================================
entry:
   cli         ; disable interrupts
   mov ax, INITSS
   mov ss, ax      
   mov sp, INITSP      
;
;
   cld         ; clear direction
   mov ax, INITSEG
   mov es, ax      ; ES == 0x2000
   xor di, di
;
   mov ax, BOOTSEG      
   mov ds, ax      ; DS == 0x07c0
   xor si, si
;
   mov cx, 0x0100      ; 256
   ;DS:SI => ES:DI
   ;BOOTSEG => INITSEG
   ;movsb == 1
   ;movsw == 2
   ;movsd == 4
   repz movsw      ; repz : do 'movsw' 256 times => 256 * 2 = 512

   jmp INITSEG:main
main:
   ; ok, we jumped from BOOT Segment
   ; so we have to set our ds to here
   mov ax, INITSEG   
   mov ds, ax

; Tell me something :)
   mov si, bootmsg
   call print

; I will load second sector to 0x2000:0x0000
;   mov ax, 0x2000      ; will be the base address of codesel
;   mov es, ax      
;   mov bx, 0x0      
;   mov al, 1      
;   mov cl, 2      
;   call read_sector

;org 0x7c00
;use16

; load Global Descriptor Table
   lgdt [gdt_ptr]
; enter the protected mode
   ; damn it damn it damn it damn it damn it
   mov eax, cr0
   or al, 1      ; toggle PE bit
;   mov eax, 1
   mov cr0, eax

;bits 16 ; <= this is not work too
   jmp DWORD codesel:psmode   ; now we have code descriptor and selector, so we will use it for far jump

bits 32
psmode:
   ; reset all data segments for executing procedure in pmode
   mov ax, datasel
   mov ds, ax
   mov ss, ax
   mov es, ax
   mov gs, ax
   mov fs, ax
   ; I don't want to terminate booting, wait user will reboot the system
pmode: jmp pmode


; ==========================================================
; Functions
; ==========================================================
bits 16
print:
   cld      ; direction forward
   lodsb      ; next character   ; ds:si
   cmp al, 0
   jz p_done;
   mov ah, 0x0E
   mov bx, 0x0007
   int 0x10
   jmp print
p_done:
   ret

waitkey:
   mov ah,0
   int 0x16
   ret

read_sector:
;  es:bx = address of destination (memory)
;  al = sector count
;  cl = sector start number
   mov ah, 0x02
   mov ch, 0
   mov dl, [bootdrive]
   mov dh, 0
   int 0x13
   jc read_sector
   ret
; End of code section, I don't want to execute below data

; =====================
; Message for user
; =====================
bootdrive   db   0
bootmsg      db   'SJOS v0.1', 13, 10, 0
pressmsg   db   'Press any key', 13, 10, 0

; =======================
; Global Descriptor Table
; =======================
gdt_ptr
   dw end_gdt - begin_gdt - 1   ; limit
   dd begin_gdt         ; base address
begin_gdt
   dd   0x00000000   ; null descriptor
   dd   0x00000000   

; these two descriptors are indicate same data, becuase we will implement the flat model
codesel equ $-begin_gdt   ; start address of code descriptor(index), we will use this for selector(cs)
code0
   dw 0xffff   ; segment limit
   dw 0x0000   ; base address
   db 0x00
   db 0x9a      ; 10011010 - priv level 0, code
   db 0xcf      ; 11001111 - granurlarity
   db 0x00      ; 00000000 - base address

datasel equ $-begin_gdt   ; start address of data descriptor(index), we will use this for selector(ds)
data0
   dw 0xffff
   dw 0x0000
   db 0x00
   db 0x92
   db 0xcf
   db 0x00
end_gdt
; ======================
;    End of GDT
; ======================

times   510 - ($ - $$)   db   0    ; fill rest area of boot sector with '0'
dw   0xAA55            ; Signature of Boot Sector
i was modify INITSS and INITSP
but it also didn't work.

what am I wrong..

Re:Help me, I can't enter the protected mode

Posted: Mon Mar 29, 2004 2:09 am
by Pype.Clicker
The things i can observe :
1. you're *assuming* that the bootloader is calling your code with CS=0x7C0 and IP=0x0000 while it could perfectly be using CS=0 and IP=0x7C00 ... Use a far jump rather than a normal jump as your first instruction.

2. provided that you're in a segment that is not 0 (INIT_SEG) when you setup your GDT, you have to adjust your GDT base (remember: this one needs to be an *absolute* address while you give it an offset within INIT_SEG.

3. copying the bootcode somewhere else is irrelevant for something that doesn't need to chain to another bootsector. If you don't plan to do such chain loading (and as you're entering pmode, you probably don't want chain loading), drop that useless stuff: it will make you more available bytes for more interresting things

Re:Help me, I can't enter the protected mode

Posted: Mon Mar 29, 2004 8:17 am
by aladdin
there is something missing in youre code, i don't think this is the cause of problem but ... ::)

Code: Select all

; Starting point
; ===============================================
entry:
   cli         ; disable interrupts
   mov ax, INITSS
   mov ss, ax      
   mov sp, INITSP      

   sti   ; reactivate ints

;i think you must put an sti instruction right after setting stack

Re:Help me, I can't enter the protected mode

Posted: Mon Mar 29, 2004 8:20 am
by DennisCGc
aladdin wrote: there is something missing in youre code, perhaps i don't think this is the cause of problem but ...

Code: Select all

; Starting point
; ===============================================
entry:
   cli         ; disable interrupts
   mov ax, INITSS
   mov ss, ax      
   mov sp, INITSP      

   sti   ; reactivate ints

;i think you must put an sti instruction right after setting stack
That AIN'T a problem.
It only has to be done, when you use int 0x13, int 0x1a, etc.
The stack is, now, interrupt independentable.
The interrupts mentioned above, are interrupt dependentable.

Re:Help me, I can't enter the protected mode

Posted: Tue Apr 06, 2004 5:50 am
by DennisCGc
It's indeed for OS USERS, not OS Developers :o
Remember rajee, this part of the forum, is created for OS developers, not OS users. ;)
go to ##spam-link##. you will get your answer

Re:Help me, I can't enter the protected mode

Posted: Tue Apr 06, 2004 6:12 am
by Pype.Clicker
okay. spam post deleted. own reaction deleted. i leave the fate of Candy and DenisGC's posts in the hand of their author (thanks for your cross-check).
I suggest we forget about ##spam-link## thingy and go back to "real" posts in "real" threads ...