Page 1 of 1

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

Posted: Wed Mar 07, 2012 10:58 am
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.

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

Posted: Wed Mar 07, 2012 11:18 am
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

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

Posted: Wed Mar 07, 2012 3:25 pm
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.

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

Posted: Wed Mar 07, 2012 7:58 pm
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.

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

Posted: Tue Mar 13, 2012 10:57 am
by shacknetisp
I'm not using GRUB.

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

Posted: Tue Mar 13, 2012 11:03 am
by Solar
In that case, you are in 16bit mode, so Brendan's advice applies.

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

Posted: Fri Mar 16, 2012 2:45 pm
by shacknetisp
I'm not in 16 bit mode. :D

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

Posted: Fri Mar 16, 2012 2:56 pm
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 ?

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

Posted: Mon Mar 19, 2012 4:06 pm
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.)

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

Posted: Mon Mar 19, 2012 4:22 pm
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.

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

Posted: Mon Mar 19, 2012 6:57 pm
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?

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

Posted: Mon Mar 19, 2012 7:34 pm
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.

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

Posted: Tue Mar 20, 2012 12:30 am
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.

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

Posted: Mon Mar 26, 2012 11:47 am
by shacknetisp
Thanks,
I will use IO ports. Also I just switched to GRUB/QEMU :D