How do I "Thunk" to 16 bit in my 32 Bit OS?

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.
Post Reply
shacknetisp
Member
Member
Posts: 29
Joined: Wed Jan 25, 2012 2:37 pm

How do I "Thunk" to 16 bit in my 32 Bit OS?

Post by shacknetisp »

How do I "Thunk" to 16 bit in my 32 Bit OS?
It is written in GNU Assembler and C, so NASM code isn't helpful.
I need to thunk to 16 bit to gain information from the bios, then pass it to the 32 bit C Code.
Thank you.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: How do I "Thunk" to 16 bit in my 32 Bit OS?

Post by Brendan »

Hi,
shacknetisp wrote:I need to thunk to 16 bit to gain information from the bios, then pass it to the 32 bit C Code.
Why didn't you gather then information during boot when you were in real mode anyway (before you switched to 32-bit)?

Otherwise, see the wiki page about real mode.


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.
User avatar
brain
Member
Member
Posts: 247
Joined: Thu Nov 05, 2009 5:04 pm
Location: UK
Discord: https://discord.gg/brainbox
GitHub: https://github.com/braindigitalis
Contact:

Re: How do I "Thunk" to 16 bit in my 32 Bit OS?

Post by brain »

Brendan wrote:Hi,
shacknetisp wrote:I need to thunk to 16 bit to gain information from the bios, then pass it to the 32 bit C Code.
Why didn't you gather then information during boot when you were in real mode anyway (before you switched to 32-bit)?

Otherwise, see the wiki page about real mode.


Cheers,

Brendan
Im guessing he's using grub like quite a few of us, in which case he never has to deal with real mode and the bios :)

Most of the stuff you need info on is passed to you in the multiboot structure if you need that, for example vesa information or memory maps.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: How do I "Thunk" to 16 bit in my 32 Bit OS?

Post by gerryg400 »

If you have just been loaded by Grub then you are probably executing at 0x100000 and the original interrupt table and real mode environment are still in place below 0x100000. Here's an example (approximately) from my loader.

It shows how thunking can be done.

Code: Select all

int vbe_controller_info(vbe_info_block_t *p_vbe_info_blk) {

    regpack16_t     reg;

    reg.ax  = 0x4f00;
    reg.di  = 0xa000;

    /* Copy the realmode code down to the first 64k */
    memcpy((void *)0x7000, &start_real,
                    (uint32_t)&start_real_end - (uint32_t)&start_real);

    /* Copy the regpack down to the real mode stack in the first 64k */
    memcpy((void *)(0xe000 - sizeof(regpack16_t)), preg, sizeof(regpack16_t));

    int10_exec();

    /* Reload the protected mode descriptor tables */
    cpu_init();

    /* Bring back the copy of the regpack */
    memcpy(&reg, (void *)(0xe000 - sizeof(regpack16_t)), sizeof(regpack16_t));

    if (reg.ax != 0x4f00)
        return -1;

    /* Copy back the data */
    memcpy(p_vbe_info_blk, (void *)0xa000, sizeof(vbe_info_blk));

    return 0;
}
It assumes a 1:1 page mapping and it's all a bit ugly with hard-coded addresses but never mind. You don't want to be switching into real mode once your OS is running so this sort of thing is only suitable during boot.
If a trainstation is where trains stop, what is a workstation ?
shacknetisp
Member
Member
Posts: 29
Joined: Wed Jan 25, 2012 2:37 pm

Re: How do I "Thunk" to 16 bit in my 32 Bit OS?

Post by shacknetisp »

I'm not using GRUB.
User avatar
Solar
Member
Member
Posts: 7635
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: How do I "Thunk" to 16 bit in my 32 Bit OS?

Post by Solar »

In that case, you are in 16bit mode, so Brendan's advice applies.
Every good solution is obvious once you've found it.
shacknetisp
Member
Member
Posts: 29
Joined: Wed Jan 25, 2012 2:37 pm

Re: How do I "Thunk" to 16 bit in my 32 Bit OS?

Post by shacknetisp »

I'm not in 16 bit mode. :D
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: How do I "Thunk" to 16 bit in my 32 Bit OS?

Post by gerryg400 »

If you haven't over-written the real mode environment and haven't made too many changes to the PC devices you should still be able to use the same scheme that I do from my Grub-launched code. Where exactly is your problem ?
If a trainstation is where trains stop, what is a workstation ?
shacknetisp
Member
Member
Posts: 29
Joined: Wed Jan 25, 2012 2:37 pm

Re: How do I "Thunk" to 16 bit in my 32 Bit OS?

Post by shacknetisp »

Basically, I need to switch from 32 bit to 16 bit dynamically, and then switch back.
(Example: printf() gets a error, and I need to use the BIOS to do a print to screen.)
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: How do I "Thunk" to 16 bit in my 32 Bit OS?

Post by gerryg400 »

shacknetisp wrote:Basically, I need to switch from 32 bit to 16 bit dynamically, and then switch back.
(Example: printf() gets a error, and I need to use the BIOS to do a print to screen.)
Brendan's post provided a link that tells you how to switch modes. My post was an attempt to show you some thunking code that could work on top of that. Have you followed Brendans link? I'm not sure what you are up to.
If a trainstation is where trains stop, what is a workstation ?
User avatar
brain
Member
Member
Posts: 247
Joined: Thu Nov 05, 2009 5:04 pm
Location: UK
Discord: https://discord.gg/brainbox
GitHub: https://github.com/braindigitalis
Contact:

Re: How do I "Thunk" to 16 bit in my 32 Bit OS?

Post by brain »

What error does printf give, you didn't use the one from the standard library that isn't available in a freestanding environment right?
User avatar
neon
Member
Member
Posts: 1568
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: How do I "Thunk" to 16 bit in my 32 Bit OS?

Post by neon »

Actually... I am going to have to suggest against going back to real mode from protected mode for printf. While it certainly is possible, doing it while the protected mode operating environment has already been configured by the operating system can be very, very hard.

I suggest v86 mode or your own display driver over falling back to real mode. If you really want to use real mode, however, gerryg400 posted an accurate way of doing it regardless of what boot loader you are using provided you are in protected mode with identity mapping and your system configuration does not conflict with what the bios expects. I also suggest against using the name "printf" for a non-crt compliant function.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: How do I "Thunk" to 16 bit in my 32 Bit OS?

Post by bluemoon »

shacknetisp wrote:(Example: printf() gets a error, and I need to use the BIOS to do a print to screen.)
If printf gets an error, which i presume it cannot access physical B80000, you better do it other way than resort to BIOS, like OUTs to serial port, etc.
shacknetisp
Member
Member
Posts: 29
Joined: Wed Jan 25, 2012 2:37 pm

Re: How do I "Thunk" to 16 bit in my 32 Bit OS?

Post by shacknetisp »

Thanks,
I will use IO ports. Also I just switched to GRUB/QEMU :D
Post Reply