OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Apr 23, 2024 9:23 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 77 posts ]  Go to page Previous  1, 2, 3, 4, 5, 6  Next
Author Message
 Post subject: Re: x86 asm bios interrupts in C
PostPosted: Sat Sep 08, 2018 10:36 pm 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

alberinfo wrote:
this is the bootloader code:


This isn't boot loader code - you're probably using GRUB as the boot loader.

alberinfo wrote:
Code:
BITS 32

section .text
    align 4
    dd 0x1BADB002
    dd 0x00
    dd - (0x1BADB002+0x00)

global start
extern main
start:
    cli
    lidt [idt_info]
    smsw ax
    and eax, 1
    je callmain
    mov ebp, esp
    push dword [ebp+4]
    push dword [ebp+8]
    pushfs
    or dword [esp], (1 << 17)
    push dword [ebp+12]
    push dword [ebp+16]
    iret

callmain:
    call main
    hlt

section .text
idt_info:
    idt_start dw 0
    idt_end dw idt_start - 1


You're using sections "strangely". Typically the ".text" section only contains executable code and not data (and data like "idt_info" should be in a ".data" or ".rodata" section). Also note that normally you'd have a special section for the multi-boot header so that you can write a linker script that ensures the multi-boot header is within the first 8 KiB of the file (which is required by multi-boot).

alberinfo wrote:
why it fails!!??


It fails to link because (with "dw idt_start - 1") you're asking for a 16-bit piece of data ("dw") that contains a 32-bit address (the result of "idt_start - 1") and the linker can't figure out how to make 32 bits of stuff fit in 16 bits of space.

Note that this is all very wrong, and that it looks like you're using a variation of "brute force coding practices" where you randomly change lines of code with no understanding of what the code is supposed to do and then wondering why nothing works. For example, you can't just set a single bit and expect virtual8086 mode to work, and "IRET to virtual 8086 mode" requires a lot more on the stack (the values to use for all segment registers). You will need to read and understand the part of the Intel manual that describes all of the details of virtual8086 mode (which includes requiring a valid GDT and a valid TSS descriptor, and a valid IDT with a general protection fault handler that emulates a bunch of "sensitive" instructions - e.g. CLI, STI, IN, OUT, HLT, ...).

alberinfo wrote:
now, there's something strange.there i call smsw ax, but why ax, and not eax??it's the code ok?


Once upon a time (80286) Intel created a horrible 16-bit protected mode that had a 16-bit "machine status word", and had an instruction (smsw) to set the machine status word. Not long after that Intel added a lot of changes (including adding support for 32-bit, adding paging, adding hardware task switching, adding virtual8086 mode, ...) and added a set of 32-bit control registers and new instructions (like "mov cr0, ...") for the new control registers. For backward compatibility the obsolete 16-bit machine status word became the lowest 16 bits of the new 32-bit "CR0" control register.

Essentially, 32-bit didn't exist when SMSW was created, and then SMSW was deprecated when 32-bit was introduced so Intel had no reason to make it work for 32-bit.

alberinfo wrote:
UPDATE: now solution is that it isnt pushfs, it's pushfd.


For NASM, there's a "warn on orphan labels" command line option (that should be enabled by default?) that you should be using to make sure that simple typos (e.g. "pushfs") don't get treated as labels (e.g. like "pushfs: ") and accepted by the assembler.


Cheers,

Brendan

_________________
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.


Top
 Profile  
 
 Post subject: Re: x86 asm bios interrupts in C
PostPosted: Tue Sep 11, 2018 12:55 pm 
Offline
Member
Member

Joined: Wed Aug 29, 2018 4:42 pm
Posts: 122
hi,
Quote:
This isn't boot loader code - you're probably using GRUB as the boot loader.


i know it isn't the bootloader, as you said, grub is the bootloader, if you see in one of my previus reply, i've put this file as "kernelloader.asm", or something like that.

in the idt_info i made a mistake, cause it was .data and i wrote it like .text :)

Quote:
Note that this is all very wrong, and that it looks like you're using a variation of "brute force coding practices"
´

don't worry, i'll learn as soon as is in my hand to make it, 'cause i don't have much time to do learn about it..
Quote:
For example, you can't just set a single bit and expect virtual8086 mode to work
i know, when i say "will it work" i mean if it won't crash or something like that... also in pushfs and pushfd, i forgot to mean, it should be, i think

anyway the osdev wiki does not contain much information about how to do it, and it's wrong. see in the code for "enabling VM86 mode" it makes an 16-bit label and another 32-bit label. also i didn't found anything by the web which explain how to do it.can you send me another link with more information please??

Thanks!


Top
 Profile  
 
 Post subject: Re: x86 asm bios interrupts in C
PostPosted: Wed Sep 12, 2018 3:06 am 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

alberinfo wrote:
anyway the osdev wiki does not contain much information about how to do it, and it's wrong. see in the code for "enabling VM86 mode" it makes an 16-bit label and another 32-bit label. also i didn't found anything by the web which explain how to do it.can you send me another link with more information please??


You're right; that wiki page is relatively horrible - the example code to enter virtual8086 mode is wrong and there's lots of things it doesn't mention. There's also a different wiki page called Virtual Monitor that does mention a lot more of the things you'll need to worry about, but it's more of an introduction than a full reference. Ideally, both of these pages would be rewritten and combined into a single "Using Virtual 8086 Mode" page; but mainly you shouldn't be relying on the wiki for more than an introduction anyway. Instead; you should read (and understand) the whole "8086 emulation" chapter of Intel's software developer's manual.

Please note that this is not something you will get working in a few days; it's something that is probably going to take a month to get working, where after that month you won't be sure if there is/isn't a "corner case" that your code (e.g. your GPF exception handler) doesn't handle correctly - you'll just reach a "works for the video cards I tested it on (but may crash on other video cards)" state (and then maybe you might spend a few more weeks writing a set of "virtual8086 tests" to try to make sure there aren't any of these corner cases).


Cheers,

Brendan

_________________
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.


Top
 Profile  
 
 Post subject: Re: x86 asm bios interrupts in C
PostPosted: Wed Sep 12, 2018 12:27 pm 
Offline
Member
Member
User avatar

Joined: Mon Jan 15, 2018 2:27 pm
Posts: 201
If you're interested, I've fabricobbled some little demo of what is actually needed to make vm86 working in 32 bit protected mode and C. Most of this code you would have to implement anyway for any OS. But if you're just starting, then there is a lot to do.


Attachments:
File comment: type make to make :) and make run to run (if you're lucky, it will just work).
vm86.tar.gz [8.67 KiB]
Downloaded 51 times
Top
 Profile  
 
 Post subject: Re: x86 asm bios interrupts in C
PostPosted: Wed Sep 12, 2018 12:39 pm 
Offline
Member
Member

Joined: Wed Aug 29, 2018 4:42 pm
Posts: 122
Thanks PVC for your file, i think it will help me a lot.but for me it isn't enought for get it working, i want to know how does it work.i used Virtual 8086 Mode and Virtual Monitor also with intel developer's manual, but i didn't get a full explanation of how to make it work.anyway its good to get a file with references :)

Thanks!


Top
 Profile  
 
 Post subject: Re: x86 asm bios interrupts in C
PostPosted: Wed Sep 12, 2018 12:42 pm 
Offline
Member
Member
User avatar

Joined: Mon Jan 15, 2018 2:27 pm
Posts: 201
Actually there is a lot of weird hacks involved so it takes time to understand. I've gotten it to work just recently myself.


Top
 Profile  
 
 Post subject: Re: x86 asm bios interrupts in C
PostPosted: Wed Sep 12, 2018 12:47 pm 
Offline
Member
Member

Joined: Wed Aug 29, 2018 4:42 pm
Posts: 122
it worked!!:)but i dont how to post the img

Thanks!


Top
 Profile  
 
 Post subject: Re: x86 asm bios interrupts in C
PostPosted: Wed Sep 12, 2018 12:50 pm 
Offline
Member
Member
User avatar

Joined: Mon Jan 15, 2018 2:27 pm
Posts: 201
You're welcome :)


Top
 Profile  
 
 Post subject: Re: x86 asm bios interrupts in C
PostPosted: Wed Sep 12, 2018 1:05 pm 
Offline
Member
Member

Joined: Wed Aug 29, 2018 4:42 pm
Posts: 122
gcc doesn't advice of any error, but LD makes: undefined reference to 'idtInitialize', also to 'vm86Initialize', 'vm86Int' (x2 times), '__stack_chk_fail' --> in some codes this happens, i dunno why.. #-o


Top
 Profile  
 
 Post subject: Re: x86 asm bios interrupts in C
PostPosted: Wed Sep 12, 2018 1:08 pm 
Offline
Member
Member
User avatar

Joined: Mon Jan 15, 2018 2:27 pm
Posts: 201
-fno-stack-protector should take care of it. are you trying to compile it with different flags?


Top
 Profile  
 
 Post subject: Re: x86 asm bios interrupts in C
PostPosted: Wed Sep 12, 2018 1:17 pm 
Offline
Member
Member

Joined: Wed Aug 29, 2018 4:42 pm
Posts: 122
i'll try with it.. im using ld -m elf_i386 -o bootloader.bin bootloader.o kernel.o


Top
 Profile  
 
 Post subject: Re: x86 asm bios interrupts in C
PostPosted: Wed Sep 12, 2018 1:19 pm 
Offline
Member
Member

Joined: Wed Aug 29, 2018 4:42 pm
Posts: 122
i tried, but when kernel boots, it comes back to grub. i'm tired of that error


Top
 Profile  
 
 Post subject: Re: x86 asm bios interrupts in C
PostPosted: Wed Sep 12, 2018 1:23 pm 
Offline
Member
Member
User avatar

Joined: Mon Jan 15, 2018 2:27 pm
Posts: 201
That, and from what I see you only have functions referenced from main.c marked as undefined. So the problem seems to be that your kernel.o is just my main.o file which won't work without other .o files. You have to link them. main.c is just a demo code.


Top
 Profile  
 
 Post subject: Re: x86 asm bios interrupts in C
PostPosted: Wed Sep 12, 2018 1:24 pm 
Offline
Member
Member

Joined: Wed Aug 29, 2018 4:42 pm
Posts: 122
lol forgot that step XD


Top
 Profile  
 
 Post subject: Re: x86 asm bios interrupts in C
PostPosted: Wed Sep 12, 2018 1:30 pm 
Offline
Member
Member

Joined: Wed Aug 29, 2018 4:42 pm
Posts: 122
aaaaahhhh linker what problem you got with me!! multiple definitions of kmain, first in kernel.c(my kernel).. the problem is that in my kernel the only reference to kmain is when i call it


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 77 posts ]  Go to page Previous  1, 2, 3, 4, 5, 6  Next

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 99 guests


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 post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group