flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Help to start my own OS

Goto page Previous  1, 2, 3, 4, 5, 6, 7  Next
Author
Thread Post new topic Reply to topic
cod3b453



Joined: 25 Aug 2004
Posts: 618
cod3b453 20 Feb 2011, 23:49
Code:
gdtr: ; GDT register entry
       dd gdt
      dw limit_gdt_size - 1

align 4 ; <<< important bits 1:0 are used for RPL when performing jmp

gdt: ; Actual GDT
        dq 0 ; 0x00 null descriptor
 ;  (  limit  ) (     base     ) (P,DPL,DT,Type)(G,DB,0,AVL,limit)(base)
     db 0xFF, 0xFF, 0x00, 0x00, 0x00,  1001'1111b,     1100'1111b,     0x00 ; 0x08 CS
  db 0xFF, 0xFF, 0x00, 0x00, 0x00,  1001'0111b,     1100'1111b,     0x00 ; 0x10 DS
limit_gdt_size = $ - gdt
    
(I've not checked the GDT entry values)
Post 20 Feb 2011, 23:49
View user's profile Send private message Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 20 Feb 2011, 23:57
what is RPL, cod?
and why i need to sub 1 in the size of the gdt?
Post 20 Feb 2011, 23:57
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 618
cod3b453 21 Feb 2011, 00:15
When switching mode:
Code:
; ...
jmp 0x0008:pmode
;...
align 4
pmode:
; ...    

bits 1:0 are used to Request a Privilege Level. (GDT entries are 8 bytes in size so bits 2:0 are ignored) The RPL can be the same or greater (less privileged) than the DPL described in the GDT entry you selected. This means you could use the same descriptor for both system and user code simply by changing the RPL.
Code:
; ...
jmp 0x000B:user_code ; switch to user code, RPL = 3
;...
; ...    

----
That value is the maximum offset which is one less than the size.
Post 21 Feb 2011, 00:15
View user's profile Send private message Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 21 Feb 2011, 00:22
this is my current boot file:

Code:
   
    mov ah, 02h         ;subfunção de leitura
    mov al, 1           ;numero de setores para ler 
    mov ch, 0           ;trilha ( cylinder ) 
    mov cl, 2           ;setor 
    mov dh, 0           ;cabeça
    mov dl, 0           ;drive ( 00h = A: ) 
    mov bx, 0800h       ;ES:BX aponta para o local da memória_
    mov es, bx          ;onde vai ser escrito os dados
    mov bx, 0           ;0800:0000h ( ES = 0800h, BX = 0000h )
    int 13h             ;interrupt de disquete

; -- GDT
        cli
        lgdt [gdtr]
        jmp 0x08:@f
align 4
@@:     mov ax, 0x10 ; 0x10 = DS Selector
        mov ds, ax
        mov es, ax
        mov fs, ax
        mov gs, ax
        mov ss, ax

; -- set PM (Protected Mode)
        mov eax, cr0
        inc ax
        mov cr0, eax
        jmp 0x8000      ; goto kernel


gdtr:   dd gdt      ; base
        dw gdt_size ; limit
align 4 ; important bits 1:0 are used for RPL when performing jmp 
gdt:
    dq 0 ; 0x00 null descriptor
    ;  (  limit  ) (     base     ) (P,DPL,DT,Type)(G,DB,0,AVL,limit)(base)
    db 0xFF, 0xFF, 0x00, 0x00, 0x00,  1001'1010b,     1100'1111b,     0x00 ; 0x08 CS
    db 0xFF, 0xFF, 0x00, 0x00, 0x00,  1001'0011b,     1100'1111b,     0x00 ; 0x10 DS
gdt_size = $ - gdt - 1
    

_________________
Sorry if bad english.
Post 21 Feb 2011, 00:22
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4354
Location: Now
edfed 21 Feb 2011, 07:46
Code:
        jmp 0x8000      ; goto kernel
    

you should jmp to a far adress in order to load CS descriptor
Code:
jmp 0008h : 8000h ; goto kernel
    

and the kernel should start in 32 bits mode
Code:
org 8000h
use32
kernel: 
mov eax,'boot' 
...    
Post 21 Feb 2011, 07:46
View user's profile Send private message Visit poster's website Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 21 Feb 2011, 10:27
edfed wrote:
you should jmp to a far adress in order to load CS descriptor


but i did here:
Code:
; -- GDT 
        cli 
        lgdt [gdtr] 
        jmp 0x08:@f ; <<<-----     

Didn't?

Thank you, ed.

_________________
Sorry if bad english.
Post 21 Feb 2011, 10:27
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4354
Location: Now
edfed 21 Feb 2011, 11:40
jmp 8:@f should be made after mov cr0,eax

Code:
        cli                     ; disable interrupts
        lgdt fword[cs:gdt.size] ; load the gdt from [cs:gdt] 6 bytes pseudo descriptor
        mov eax,cr0             ; equivalent to "or cr0,1"
        or al,1                 ;   switches the CPU in protected mode-
        mov cr0,eax             ;   protected mode enable
        jmp gdt.code:.pmode     ; equivalent to "mov cs,gdt.data" + "mov ip,.pmode"
.pmode:                         ;   the first instruction right after pm enable
        use32                   ; code below is 32 bits
        mov ax,gdt.data         ;
        mov ds,ax               ; make ds = .data entry in gdt, flat linear adress space
        mov word[0b8000h],7441h ; put a red char 'A' in upper left corner, on grey background, just to show it works
        hlt                     ; halts the processor, then, it will consume less energy
        jmp $                   ; infinite loop
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        align 8                 ; align on 8 byte boundary for optimal performance
gdt:    dw 0                    ; in order to align dword part of pseudo desciptor on dword boundary
.size   dw @f-gdt-1             ; word part of pseudo desciptor, size of gdt in bytes
.linear dd gdt                  ; dword part of pseudo descriptor, linear base adress
.code=$-gdt                     ; first entry in gdt (8*1)
dw 0ffffh,0                     ;   4Gbytes, start at linear 0
db 0,10011010b,11001111b,0      ;   granularity = 64Kbytes, code segment, ring 0, read only,etc...
.data=$-gdt                     ; second entry in gdt (8*2)
dw 0ffffh,0                     ;   4Gbytes, start at linear 0
db 0,10010010b,11001111b,0      ;   granularity = 64Kbytes, data segment, ring 0, read/write,etc...
@@:                             ; used for gdt.size calculation
    
Post 21 Feb 2011, 11:40
View user's profile Send private message Visit poster's website Reply with quote
christiandy



Joined: 03 Mar 2011
Posts: 25
Location: 101
christiandy 03 Mar 2011, 15:50
I designing an operating system too. Can someone give me an example of 32 bit FAT32 boot loader?
Post 03 Mar 2011, 15:50
View user's profile Send private message AIM Address Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 03 Mar 2011, 16:20
Post 03 Mar 2011, 16:20
View user's profile Send private message Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 05 Mar 2011, 19:20
I wanna make some changes in my boot, and i did, but now i think my GDT isn't working, when i jmp 8:@f i get restarted..

Code:
format binary as 'img'

; --------------------------------------------------------------------------
; -- Constants
; --------------------------------------------------------------------------

    SECTOR_BOOT    = 0x7C00
    SECTOR_KERNEL  = 0x1000
    BIOS_SIGNATURE = 0xAA55

; --------------------------------------------------------------------------
; -- Boot
; --------------------------------------------------------------------------

org 0
use16

; -- Read floppy disk

    @@: mov ah,0           ; Reset floppy disk function
        mov dl,0           ; drive 0 = floppy drive
        int 0x13           ;
        jc  @b             ; CF: 1 error, 0 ok

        mov bx,0x1000      ; ES:BX Buffer to read sectors to
        mov es,bx          ; 0800:0000h ( ES = 0800h, BX = 0000h )
        xor bx,bx
        mov ah,2           ; Reading Sectors
        mov al,1           ; number of sectors to read
        mov ch,0           ; track (cylinder) number
        mov cl,2           ; sector (bits 0-5). (bits 6-7 HD only)
        mov dh,0           ; head number
        mov dl,0           ; drive number (00h = A: ) (bit 7 set for hard disks)
        int 0x13           ; Return: AH = status, AL = number of sectors read, CF = 1 failure, 0 successful

        mov dx,0x03F2      ; -- Turn floppy off
        mov al,0
        out dx,al

; -- reset segs

        xor ax,ax
        mov ds,ax
        mov es,ax
        mov ss,ax
        mov sp,SECTOR_BOOT

; -- VBE

        mov di, ModeInfoBlock
        mov ax, 0x4F01
        mov cx, 0x4145 ; 145 = 32bits
        int 10h

        ;cmp byte[ModeInfoBlock.BitsPerPixel],32
        ;je $

        mov ax, 0x4F02
        mov bx, 0x4145  ; 0x4000 = set LFB; 145 = 1280*1024*32bits
        int 10h

        jmp SECTOR_KERNEL:0

        ; Fill this sector up  
        rb 510-($-$$)
        dw BIOS_SIGNATURE


; --------------------------------------------------------------------------
; -- Kernel 16bits
; --------------------------------------------------------------------------

org SECTOR_KERNEL

    kernel_16:

; -- A20 enable

        mov ax,0x2401
        int 0x15

; -- load GDT

        cli
        lgdt [gdtr]

; -- enable PM (Protected Mode)

        mov eax, cr0
        inc ax
        mov cr0, eax    ; enable PM
        jmp 0x08:@f    ############## i get restarted here ###########
@@:
use32
        hlt
        jmp $   

        mov ax, 0x10    ; GDT_DATA_SEGMENT_SELECTOR
        mov ds, ax
        mov es, ax
        mov fs, ax
        mov gs, ax
        mov ss, ax
        mov esp,0x7C00

; -- Kernel Go!!!


; .... ### A LOT OF MORE CODES HERE ###


; -- Then GDT:

align 4

gdtr:   dw gdt_size ; limit
        dd gdt      ; base

align 4 ; important bits 1:0 are used for RPL when performing jmp
gdt:
        dq 0 ; 0x00 null descriptor
        ;  (  limit  ) (     base     ) (P,DPL,DT,Type)(G,DB,0,AVL,limit)(base)
        db 0xFF, 0xFF, 0x00, 0x00, 0x00,  1001'1010b,     1100'1111b,     0x00 ; 0x08 code segment selector
        db 0xFF, 0xFF, 0x00, 0x00, 0x00,  1001'0011b,     1100'1111b,     0x00 ; 0x10 data segment selector
gdt_size = $-gdt-1     

     

_________________
Sorry if bad english.
Post 05 Mar 2011, 19:20
View user's profile Send private message Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 06 Mar 2011, 01:05
i found this table:

Code:
0x00000000 - 0x000003FF - Real Mode Interrupt Vector Table
0x00000400 - 0x000004FF - BIOS Data Area
0x00000500 - 0x00007BFF - Unused
0x00007C00 - 0x00007DFF - Our Bootloader
0x00007E00 - 0x0009FFFF - Unused
0x000A0000 - 0x000BFFFF - Video RAM (VRAM) Memory
0x000B0000 - 0x000B7777 - Monochrome Video Memory
0x000B8000 - 0x000BFFFF - Color Video Memory
0x000C0000 - 0x000C7FFF - Video ROM BIOS
0x000C8000 - 0x000EFFFF - BIOS Shadow Area
0x000F0000 - 0x000FFFFF - System BIOS    

In PM can I override this data?

PS: i'm still getting restart.
Post 06 Mar 2011, 01:05
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4354
Location: Now
edfed 06 Mar 2011, 02:53
Code:
YES!
0x00000000 - 0x000003FF - Real Mode Interrupt Vector Table
0x00000400 - 0x000004FF - BIOS Data Area
0x00000500 - 0x00007BFF - Unused
0x00007C00 - 0x00007DFF - Our Bootloader
0x00007E00 - 0x0009FFFF - Unused
direct to screen VGA framebuffer
0x000A0000 - 0x000BFFFF - Video RAM (VRAM) Memory
0x000B0000 - 0x000B7777 - Monochrome Video Memory
0x000B8000 - 0x000BFFFF - Color Video Memory
NO! BIOS etc...
0x000C0000 - 0x000C7FFF - Video ROM BIOS
0x000C8000 - 0x000EFFFF - BIOS Shadow Area
0x000F0000 - 0x000FFFFF - System BIOS
    
Post 06 Mar 2011, 02:53
View user's profile Send private message Visit poster's website Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 618
cod3b453 06 Mar 2011, 12:04
Looks like you need to align your 32bit code to a 4 byte boundary.
Post 06 Mar 2011, 12:04
View user's profile Send private message Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 06 Mar 2011, 12:51
Hmm... so i can use up to 0x000BFFFF adress, nice Smile

i did attach all my kernel (and boot, etc) in a single file, please help me, i don't know why im getting restarted.


Description:
Download
Filename: mykernel.asm
Filesize: 13.86 KB
Downloaded: 270 Time(s)


_________________
Sorry if bad english.
Post 06 Mar 2011, 12:51
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4354
Location: Now
edfed 06 Mar 2011, 13:36
no, the memory used for VGA frame buffer cannot be used for general purpose because it is mapped in the video card, and then, it is slow memory.

the only memory you can use is the first 640 KB (returned by CMOS reg 15h 16h.
Code:
mov al,15h
out 71h,al
in al,70h
mov [lowmem],al
mov al,16h
out 71h,al
in al,70h
mov [lowmem+1],al
;word[lowmem]=640
    


and all memory after 1MB.
Post 06 Mar 2011, 13:36
View user's profile Send private message Visit poster's website Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 07 Mar 2011, 00:59
i see.. thanks.


Hi. This is bochs output log (only error and panic events enabled):
Code:
00014091086e[WGUI ]00014600000e[WGUI ] Sim client size(720, 333) != stretched size(720, 400)!
00028183098e[WGUI ] Sim client size(726, 432) != stretched size(1280, 1024)!
00028183098e[WGUI ] Sim client size(1276, 1020) != stretched size(1280, 1024)!
00028184021e[CPU0 ] jump_protected: gate type 3 unsupported
00028184021e[CPU0 ] interrupt(): gate descriptor is not valid sys seg (vector=0x0d)
00028184021e[CPU0 ] interrupt(): gate descriptor is not valid sys seg (vector=0x08)
00028184021e[CPU0 ] exception(): 3rd (13) exception with no resolution, shutdown status is 00h, resetting
00028800000e[WGUI ] Sim client size(720, 333) != stretched size(720, 400)!
00035600000p[WGUI ] >>PANIC<< POWER button turned off.    


And this is the log of a test i'm doing to try to solve this problem (just boot and PM change):
Code:
00014132869e[CPU0 ] jump_protected: gate type 0 unsupported
00014132869e[CPU0 ] interrupt(): gate descriptor is not valid sys seg (vector=0x0d)
00014132869e[CPU0 ] interrupt(): gate descriptor is not valid sys seg (vector=0x08)
00014132869e[CPU0 ] exception(): 3rd (13) exception with no resolution, shutdown status is 00h, resetting
00015400000p[WGUI ] >>PANIC<< POWER button turned off.    

but i changed the value and nothing happened. Sad

_________________
Sorry if bad english.
Post 07 Mar 2011, 00:59
View user's profile Send private message Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 07 Mar 2011, 15:13
we solved the problem.

before:
Code:
format binary as 'img'

use16
org 0
        jmp 0x7c0:@f
@@:
        mov ax,cs
        mov ds,ax
        mov ss,ax
        mov sp,512-1-2

    @@: mov ah,0           ; Reset floppy disk function
        mov dl,0           ; drive 0 = floppy drive
        int 0x13           ;
        jc  @b             ; CF: 1 error, 0 ok

        mov ax,0x0050
        mov bx,0x0000
        mov es,ax
        mov ah,0x02
        mov al,1      ; number of sectors to read (1-128 dec.)
        mov ch,0      ; track/cylinder number (0-1023 dec.)
        mov cl,2      ; sector number (1-17 dec.)
        mov dx,0
        int 0x13      ; http://www.stanislavs.org/helppc/int_13-2.html
        jc  @b

        jmp 0x50:kernel16

        rb 510-($-$$)
        dw 0xAA55

use16
org 0

    kernel16:

        mov ax,cs
        mov ds,ax
        mov es,ax
        mov ss,ax
        mov sp,512

        mov ax,0x2401
        int 0x15

        cli
        lgdt [gdtr]

        mov eax,cr0
        or  eax,1
        mov cr0,eax

        jmp 0x08:kernel32

align 4
  gdtr: dw gdt_size
        dd gdt
align 4
   gdt: dq 0
        db 0xFF,0xFF, 0x00,0x00,0x00, 1001'1010b, 1100'1111b, 0x00 ; 0x08 code segment selector
        db 0xFF,0xFF, 0x00,0x00,0x00, 1001'0010b, 1100'1111b, 0x00 ; 0x10 data segment selector
   gdt_size = $-gdt-1

;org 0
use32
align 4

    kernel32:

        mov eax,0x10
        mov ds,ax
        mov es,ax
        mov fs,ax
        mov gs,ax
        mov ss,ax
        mov esp,0x00FFFFFF

        hlt
        jmp $

        times 512-($-kernel16) db 0          

after:
Code:
format binary as 'img'

use16
org 0x7C00

        mov ax,cs
        mov ds,ax
        mov ss,ax
        mov sp,512-1-2

    @@: mov ah,0           ; Reset floppy disk function
        mov dl,0           ; drive 0 = floppy drive
        int 0x13           ;
        jc  @b             ; CF: 1 error, 0 ok

        mov ax,0x0050
        mov bx,0x0000
        mov es,ax
        mov ah,0x02
        mov al,1      ; number of sectors to read (1-128 dec.)
        mov ch,0      ; track/cylinder number (0-1023 dec.)
        mov cl,2      ; sector number (1-17 dec.)
        mov dx,0
        int 0x13      ; http://www.stanislavs.org/helppc/int_13-2.html
        jc  @b

        jmp kernel16

        rb 510-($-$$)
        dw 0xAA55

use16
org 0x0500

    kernel16:

        mov ax,cs
        mov ds,ax
        mov es,ax
        mov ss,ax
        mov sp,512

        mov ax,0x2401
        int 0x15

        cli
        lgdt [gdtr]

        mov eax,cr0
        or  eax,1
        mov cr0,eax

        jmp 0x08:kernel32

align 4
  gdtr: dw gdt_size
        dd gdt
align 4
   gdt: dq 0
        db 0xFF,0xFF, 0x00,0x00,0x00, 1001'1010b, 1100'1111b, 0x00 ; 0x08 code segment selector
        db 0xFF,0xFF, 0x00,0x00,0x00, 1001'0010b, 1100'1111b, 0x00 ; 0x10 data segment selector
   gdt_size = $-gdt-1

;org 0
use32
align 4

    kernel32:

        mov eax,0x10
        mov ds,ax
        mov es,ax
        mov fs,ax
        mov gs,ax
        mov ss,ax
        mov esp,0x00FFFFFF

        hlt
        jmp $

        times 512-($-kernel16) db 0          

basicaly i change the orgs and the jmp 0x50:kernel16. But i didn't understand why this happens. I would like to know.

Also: how do i know where my kernel32 is loaded? bc there is no org on it.

_________________
Sorry if bad english.
Post 07 Mar 2011, 15:13
View user's profile Send private message Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 09 Mar 2011, 11:16
where is everyone? i miss you guys Sad

i have many questions Smile

it seems my "OS" will get very bigger ( .bmp files ;/ ) so i can't put it in a floppy. how can i boot it from CD or HD? (so that Bochs can emulate)

_________________
Sorry if bad english.
Post 09 Mar 2011, 11:16
View user's profile Send private message Reply with quote
egos



Joined: 10 Feb 2009
Posts: 144
egos 09 Mar 2011, 14:31
Heh, guys read the books to answer your questions Smile

See EDD Spec., El'Torito Spec., ISO 9660 Description for more info to make bootable CD.
Post 09 Mar 2011, 14:31
View user's profile Send private message Reply with quote
Coty



Joined: 17 May 2010
Posts: 553
Location: &#9216;
Coty 09 Mar 2011, 15:25
You might also be interested in this?

http://archive.asm4u.net/Frederic_cdfs.asm

(right click, save as) If you don't understand some of the syntax you may want to look at the HyASM Manuel, I think it is a good example of no FDD emulation CD Very Happy

_________________
http://codercat.org/
Post 09 Mar 2011, 15:25
View user's profile Send private message Send e-mail Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2, 3, 4, 5, 6, 7  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.