First, can't link, now can't get into PMode

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Tom

First, can't link, now can't get into PMode

Post by Tom »

I've been having linking probs, now PMode probs. EVERY
time I try to go into PMode no matter what code i'm using ( tried 20 tutorials ) it triple falts my CPU. After this, I think I won't need help all the time. Thank you



Tom :o
frank

Re:First, can't link, now can't get into PMode

Post by frank »

jep, I know what that's like... had that problem to ;)
show us the code, else we can't help you...
Tom

Re:First, can't link, now can't get into PMode

Post by Tom »

well I tried this code, it's attached.

I did:

nasm -f bin -o asmboot.bin asmboot.asm
dd if=asmboot.bin of=/dev/fd0

And, to frank, Boy you've been on a long time.
I have only 3 hours on the computer ;)
Tom

Re:First, can't link, now can't get into PMode

Post by Tom »

[attachment deleted by admin]
Krom

Re:First, can't link, now can't get into PMode

Post by Krom »

[attachment deleted by admin]
frank

Re:First, can't link, now can't get into PMode

Post by frank »

hmmm its kinda weird coded :P

use these steps:

1. cli: Disable interrupts, because the installed interrupts are all written for real mode and if an interrupt would occur after the mode switch, your system would probably reboot.
2. Load the GDTR using lgdt, to set up the GDT.
3. Execute a mov CR0 instruction to set the PE bit of control register 0.
4. Immediately after the mov,cr0 instruction perform a far jump to clear the instruction prefetch queue, because it's still filled with real mode instructions and addresses.
5. Reload all the segment registers except CS. (which is reloaded by the far jump)
6. Load the Interrupt descriptor tables to make interrupts possible
7. sti: Re-enable interrupts.
Tom

Re:First, can't link, now can't get into PMode

Post by Tom »

Well, It didn't work :( . I tried, and it still reboots the computer). I'm using a Anthlon 1.0 GHz. here is the code:

[SECTION .text]
[BITS 16]
[GLOBAL start]
start:

cli
xor ebx,ebx ; now in real mode (16-bit)
   mov bx,cs
   shl ebx,4
   mov eax,ebx      ; EAX=EBX=CS<<4
   lea eax,[ebx]
   mov [gdt2 + 2],ax   ; set descriptor base address=EAX
   mov [gdt3 + 2],ax
   shr eax,16
   mov [gdt2 + 4],al
   mov [gdt3 + 4],al
   mov [gdt2 + 7],ah
   mov [gdt3 + 7],ah
   lea eax,[ebx + gdt]   ; point gdt_ptr to the gdt
   mov [gdt_ptr + 2],eax   ; EAX=linear address of gdt
   push dword 0      ; zero EFLAGS (interrupts off,
   popfd         ; IOPL=0, NT bit=0)
   lgdt [gdt_ptr]
   mov eax,cr0
   or al,1
   mov cr0,eax
   jmp SYS_CODE_SEL:do_pm
[BITS 32]
do_pm: mov ax,SYS_DATA_SEL ; now in 32-bit pmode
   mov ds,eax      ; EAX works, one byte smaller :)
   mov ss,eax
nop
   mov es,eax
   mov fs,eax
   mov gs,eax
   xor eax,eax      ; zero top 16 bits of ESP
   mov ax,sp
   mov esp,eax

   jmp $ ; freeze

[SECTION .data]
; null descriptor
gdt:   dw 0         ; limit 15:0
   dw 0         ; base 15:0
   db 0         ; base 23:16
   db 0         ; type
   db 0         ; limit 19:16, flags
   db 0         ; base 31:24
; linear data segment descriptor
LINEAR_SEL   equ   $-gdt
dw 0xFFFF ; limit 0xFFFFF (1 meg? 4 gig?)
   dw 0         ; base for this one is always 0
   db 0
   db 0x92         ; present,ring 0,data,expand-up,writable
db 0xCF ; page-granular (4 gig limit), 32-bit
   db 0
; code segment descriptor
SYS_CODE_SEL   equ   $-gdt
gdt2:   dw 0xFFFF
   dw 0         ; (base gets set above)
   db 0
   db 0x9A         ; present,ring 0,code,non-conforming,readable
   db 0xCF
   db 0
; data segment descriptor
SYS_DATA_SEL   equ   $-gdt
gdt3:   dw 0xFFFF
   dw 0         ; (base gets set above)
   db 0
   db 0x92         ; present,ring 0,data,expand-up,writable
   db 0xCF
   db 0
gdt_end:

gdt_ptr:
   dw gdt_end - gdt - 1   ; GDT limit
   dd gdt         ; linear, physical address of GDT
K.J.

Re:First, can't link, now can't get into PMode

Post by K.J. »

I've just quickly browsed over your code and saw a:
[tt]ORG 0[/tt]

That needs to be:
[tt]ORG 0x7C00[/tt]

Anyway, looking over the second code u posted, try something like this for setting up the GDT and enabling PMode:

[tt]
[org 0x7C00]
[section .text]
[bits 16]

   ; code for enabling the A20 line goes here

   lgdt [gdt_ptr]

   mov ax,LINEAR_DATA_SEL
   mov ds,ax
   mov es,ax
   mov ss,ax
   mov fs,ax
   mov gs,ax

   mov eax, CR0
   or eax, 0x1   ; set protected bit to 1
   move CR0, eax

   jmp LINEAR_CODE_SEL:gdt_done ; this sets up CS

[bits 32]
pmode_time:
   ; more code goes here :)

[section .data]

gdt:         ;our descriptors
; NULL descriptor
   dw 0      ; limit 15:0
   dw 0      ; base 15:0
   db 0      ; base 23:16
   db 0      ; type
   db 0      ; limit 19:16, flags
   db 0      ; base 31:24

; unused descriptor
   dw 0
   dw 0
   db 0
   db 0
   db 0
   db 0

LINEAR_DATA_SEL   equ   $-gdt
   dw 0FFFFh
   dw 0
   db 0
   db 92h      ; present, ring 0, data, expand-up, writable
   db 0CFh      ; page-granular (4 gig limit), 32-bit
   db 0

LINEAR_CODE_SEL   equ   $-gdt
   dw 0FFFFh
   dw 0
   db 0
   db 9Ah      ; present,ring 0,code,non-conforming,readable
   db 0CFh      ; page-granular (4 gig limit), 32-bit
   db 0

gdt_end:

gdt_ptr:
   dw gdt_end - gdt - 1
   dd gdt
[/tt]

The only thing about this code is that the descriptors are assuming the A20 line is enabled(thus the "code for enabling the A20 line goes here" comment). I've not tested this code, but it should work. This code does not however setup a stack.

I hope this helps(and that there are no bugs in my code there),
K.J.
Tom

Re:First, can't link, now can't get into PMode

Post by Tom »

Well, I tried part of your code ( because it didn't compile, i tried the mov cr0 things and the jmp without the :gdt_done ) and ... IT WORKED THANK YOU ;D ;D ;D. but it keeps reading the floppy after pmode. I'll post my code after updating my os to prekernel 0.2.

I need to know how to stop it from keep reading the floppy.

when I post the code, put the asmkernel.bin file on a floppy to see what i'm talking about.
Tom

Re:First, can't link, now can't get into PMode

Post by Tom »

this is the place to download the asmkernel.bin & source.http://prdownloads.sourceforge.net/fritzos/Prekernel0-2.zip?download
Tim

Re:First, can't link, now can't get into PMode

Post by Tim »

It's not still reading the floppy, it's just the motor that keeps spinning. Because you've just entered protected mode, the piece of code in the BIOS that shuts of the motor doesn't get run.

To shut off the motor, do this:

Code: Select all

mov edx,0x3f2
mov al,0x0c
out dx,al
Tom

Re:First, can't link, now can't get into PMode

Post by Tom »

THANK YOU ;D ;D ;D. You won't belive how happy I am to have a simple PMode OS working, without bugs ( the floppy motor thing ).

Is my code enough to load a gcc kernel?
Also, how do I put & get a pixel in graphics mode 13h ( 320x320x256 ) in PMode?

Thank you,
Tom :D
Tom

Re:First, can't link, now can't get into PMode

Post by Tom »

Is there a list of PMode operations that are the same as using the BIOS, I mean like this

in PMode:

mov ax, 13h
int 10h

would be:

<some things>

Is there a reference like that or somthing?
Thank you
Tom
Tim

Re:First, can't link, now can't get into PMode

Post by Tim »

There's no one reference that translates BIOS interrupt calls to raw code. All the BIOS does is provide a friendly interface to the hardware. Since the BIOS isn't available to you in protected mode, you have to write drivers for each piece of hardware (which is why Windows, Linux etc. need to provide drivers for each kind of device).

For example, for the int 10h functions, you should look for information on programming the VGA card. For int 13h, look up the floppy drive or IDE controller.
Tom

Re:First, can't link, now can't get into PMode

Post by Tom »

Hi,

I have the A20 working on my OS in PMode, but haven't released it yet.

But, what's the easiest was to use the BIOS in PMode?

Thank you.
Post Reply