Loading 64 bit mode from C code in 32 bit mode

I tried to use in C “extern void (function_name)” then call that function in my C code. It got to the assembly code that i used the extern on. Problem was it setted up 64 bit mode but when it needed to jump to the long mode it failed. All code is compiled right and other stuff is all correct, only problem is with the jmp

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>//Problem: jmp 0x08:init_lm
extern void _init_load_lm(void);
void main(void)
{
_init_load_lm();
*(char*)0xb8000 = 'Q';
for(;;);
}
// Assembly code:
bits 32
global _init_load_lm
_init_load_lm:
;Enable PAE
mov eax, cr4
or eax, 1 << 5
mov cr4, eax
;# Optional : Enable global-page mechanism by setting CR0.PGE bit to 1
mov eax, cr4
or eax, 1 << 7
mov cr4, eax
;Load CR3 with PML4 base address
;NB: in some examples online, the address is not offseted as it seems to
;be in the proc datasheet (if you were wondering about this strange thing).
mov eax, 0x1000
mov cr3, eax
;Set LME bit in EFER register (address 0xC0000080)
mov ecx, 0xC0000080 ;operand of 'rdmsr' and 'wrmsr'
rdmsr ;read before pr ne pas écraser le contenu
or eax, 1 << 8 ;eax : operand de wrmsr
wrmsr
;Enable paging by setting CR0.PG bit to 1
mov eax, cr0
or eax, (1 << 31)
mov cr0, eax
;Load 64-bit GDT
lgdt [gdt64_descriptor]
;Jump to code segment in 64-bit GDT
jmp 0x8:init_lm
bits 64
init_lm
mov ax, 0x10
mov fs, ax ;other segments are ignored
mov gs, ax
mov rbp, 0x90000 ;set up stack
mov rsp, rbp
mov al, 'E'
mov ah, 0x0f
mov [0xb8000], ax
ret ; return back to c code where it should be called from
bits 32
GDT64
;null;
dq 0x0
;code
dd 0x0
db 0x0
db 0b10011000
db 0b00100000
db 0x0
;data
dd 0x0
db 0x0
db 0b10010000
db 0b00000000
db 0x0
gdt64_descriptor:
dw $ - GDT64 - 1 ;16-bit size
dd GDT64 ;32-bit start address
// Edit: Page table
[bits 32]
build_page_tables:
;PML4 starts at 0x1000
;il faut laisser la place pour tte la page PML4/PDP/PD ie. 0x1000
;PML4 @ 0x1000
mov eax, 0x2000 ;PDP base address
or eax, 0b11 ;P and R/W bits
mov ebx, 0x1000 ;MPL4 base address
mov [ebx], eax
;PDP @ 0x2000; maps 64Go
mov eax, 0x3000 ;PD base address
mov ebx, 0x2000 ;PDP physical address
mov ecx, 64 ;64 PDP
build_PDP:
or eax, 0b11
mov [ebx], eax
add ebx, 0x8
add eax, 0x1000 ;next PD page base address
loop build_PDP
;PD @ 0x3000 (ends at 0x4000, fits below 0x7c00)
; 1 entry maps a 2MB page, the 1st starts at 0x0
mov eax, 0x0 ;1st page physical base address
mov ebx, 0x3000 ;PD physical base address
mov ecx, 512
build_PD:
or eax, 0b10000011 ;P + R/W + PS (bit for 2MB page)
mov [ebx], eax
add ebx, 0x8
add eax, 0x200000 ;next 2MB physical page
loop build_PD
;(tables end at 0x4000 => fits before Bios boot sector at 0x7c00)
ret
</code>
<code>//Problem: jmp 0x08:init_lm extern void _init_load_lm(void); void main(void) { _init_load_lm(); *(char*)0xb8000 = 'Q'; for(;;); } // Assembly code: bits 32 global _init_load_lm _init_load_lm: ;Enable PAE mov eax, cr4 or eax, 1 << 5 mov cr4, eax ;# Optional : Enable global-page mechanism by setting CR0.PGE bit to 1 mov eax, cr4 or eax, 1 << 7 mov cr4, eax ;Load CR3 with PML4 base address ;NB: in some examples online, the address is not offseted as it seems to ;be in the proc datasheet (if you were wondering about this strange thing). mov eax, 0x1000 mov cr3, eax ;Set LME bit in EFER register (address 0xC0000080) mov ecx, 0xC0000080 ;operand of 'rdmsr' and 'wrmsr' rdmsr ;read before pr ne pas écraser le contenu or eax, 1 << 8 ;eax : operand de wrmsr wrmsr ;Enable paging by setting CR0.PG bit to 1 mov eax, cr0 or eax, (1 << 31) mov cr0, eax ;Load 64-bit GDT lgdt [gdt64_descriptor] ;Jump to code segment in 64-bit GDT jmp 0x8:init_lm bits 64 init_lm mov ax, 0x10 mov fs, ax ;other segments are ignored mov gs, ax mov rbp, 0x90000 ;set up stack mov rsp, rbp mov al, 'E' mov ah, 0x0f mov [0xb8000], ax ret ; return back to c code where it should be called from bits 32 GDT64 ;null; dq 0x0 ;code dd 0x0 db 0x0 db 0b10011000 db 0b00100000 db 0x0 ;data dd 0x0 db 0x0 db 0b10010000 db 0b00000000 db 0x0 gdt64_descriptor: dw $ - GDT64 - 1 ;16-bit size dd GDT64 ;32-bit start address // Edit: Page table [bits 32] build_page_tables: ;PML4 starts at 0x1000 ;il faut laisser la place pour tte la page PML4/PDP/PD ie. 0x1000 ;PML4 @ 0x1000 mov eax, 0x2000 ;PDP base address or eax, 0b11 ;P and R/W bits mov ebx, 0x1000 ;MPL4 base address mov [ebx], eax ;PDP @ 0x2000; maps 64Go mov eax, 0x3000 ;PD base address mov ebx, 0x2000 ;PDP physical address mov ecx, 64 ;64 PDP build_PDP: or eax, 0b11 mov [ebx], eax add ebx, 0x8 add eax, 0x1000 ;next PD page base address loop build_PDP ;PD @ 0x3000 (ends at 0x4000, fits below 0x7c00) ; 1 entry maps a 2MB page, the 1st starts at 0x0 mov eax, 0x0 ;1st page physical base address mov ebx, 0x3000 ;PD physical base address mov ecx, 512 build_PD: or eax, 0b10000011 ;P + R/W + PS (bit for 2MB page) mov [ebx], eax add ebx, 0x8 add eax, 0x200000 ;next 2MB physical page loop build_PD ;(tables end at 0x4000 => fits before Bios boot sector at 0x7c00) ret </code>
//Problem: jmp 0x08:init_lm

extern void _init_load_lm(void);

void main(void) 
{
    _init_load_lm();
    *(char*)0xb8000 = 'Q';
    for(;;);
}

// Assembly code:

bits 32

global _init_load_lm

_init_load_lm:    
    ;Enable PAE
    mov eax, cr4                 
    or eax, 1 << 5               
    mov cr4, eax

    ;# Optional : Enable global-page mechanism by setting CR0.PGE bit to 1
    mov eax, cr4                 
    or eax, 1 << 7               
    mov cr4, eax

    ;Load CR3 with PML4 base address
    ;NB: in some examples online, the address is not offseted as it seems to
    ;be in the proc datasheet (if you were wondering about this strange thing).
    mov eax, 0x1000
    mov cr3, eax

    ;Set LME bit in EFER register (address 0xC0000080)
    mov ecx, 0xC0000080     ;operand of 'rdmsr' and 'wrmsr'
    rdmsr                   ;read before pr ne pas écraser le contenu
    or eax, 1 << 8          ;eax : operand de wrmsr
    wrmsr

    ;Enable paging by setting CR0.PG bit to 1
    mov eax, cr0
    or eax, (1 << 31)
    mov cr0, eax

    ;Load 64-bit GDT
    lgdt [gdt64_descriptor]

    ;Jump to code segment in 64-bit GDT
    jmp 0x8:init_lm

bits 64

init_lm
    mov ax, 0x10
    mov fs, ax          ;other segments are ignored
    mov gs, ax

    mov rbp, 0x90000    ;set up stack
    mov rsp, rbp

    mov al, 'E'
    mov ah, 0x0f
    mov [0xb8000], ax

    ret                 ; return back to c code where it should be called from

bits 32
GDT64
;null;
    dq 0x0

;code
    dd 0x0
    db 0x0
    db 0b10011000   
    db 0b00100000
    db 0x0

;data
    dd 0x0
    db 0x0
    db 0b10010000   
    db 0b00000000
    db 0x0

gdt64_descriptor:
    dw $ - GDT64 - 1        ;16-bit size
    dd GDT64                ;32-bit start address


// Edit: Page table

[bits 32]
build_page_tables:
    ;PML4 starts at 0x1000
    ;il faut laisser la place pour tte la page PML4/PDP/PD ie. 0x1000

    ;PML4 @ 0x1000
    mov eax, 0x2000         ;PDP base address            
    or eax, 0b11            ;P and R/W bits
    mov ebx, 0x1000         ;MPL4 base address
    mov [ebx], eax

    ;PDP @ 0x2000; maps 64Go
    mov eax, 0x3000         ;PD base address
    mov ebx, 0x2000         ;PDP physical address   
    mov ecx, 64             ;64 PDP

    build_PDP:
        or eax, 0b11    
        mov [ebx], eax
        add ebx, 0x8
        add eax, 0x1000     ;next PD page base address
        loop build_PDP

    ;PD @ 0x3000 (ends at 0x4000, fits below 0x7c00)
    ; 1 entry maps a 2MB page, the 1st starts at 0x0
    mov eax, 0x0            ;1st page physical base address     
    mov ebx, 0x3000         ;PD physical base address
    mov ecx, 512                        

    build_PD:
        or eax, 0b10000011      ;P + R/W + PS (bit for 2MB page)
        mov [ebx], eax
        add ebx, 0x8
        add eax, 0x200000       ;next 2MB physical page
        loop build_PD

    ;(tables end at 0x4000 => fits before Bios boot sector at 0x7c00)
    ret

New contributor

Magic Magician is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật