pmode jump to code question

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
kubeos
Member
Member
Posts: 138
Joined: Tue Jan 30, 2007 2:31 pm
Location: Kamloops BC, CANADA
Contact:

pmode jump to code question

Post by kubeos »

Hi,

I finally am starting to rewrite Kube in 32bits, but I have a question. In real mode I could jump to a newly loaded program by doing:
jmp 0x3000:0x0000

How do I do the same in Pmode?

My kernel is loaded to linear 0x10000 by the bootloader. There are no other defined segments, just a big 4gigs with both code and data. So could I just issue a jmp 0x30000:0x00000 in pmode? And if I did a call there could I just issue a retf to return back to my kernel?

Thanks, and yes I am a total pmode newbie. :)
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Hi,

In PMode, all segment selectors point to an entry in your GDT. I assume you have done the 'usual' of including one NULL segment, one Code segment and one Data segment.

You get the segment selector by the offset in to the GDT. As each entry is 8bytes long, that means, that if you have the setup described above, your CS should be 0x08. So, in this case, you would do:

Code: Select all

jmp 0x08:0x300000
I assume you want 0x300000 linear, as this is what your code sample would imply. In fact, if you are already in segment 0x08, you don't even need to far jump. Simply do the following:

Code: Select all

jmp 0x300000
You have already, of course, have done a far jump to 0x08:something to finalise going in to pmode, havent you?

Cheers,
Adam
User avatar
salil_bhagurkar
Member
Member
Posts: 261
Joined: Mon Feb 19, 2007 10:40 am
Location: India

Post by salil_bhagurkar »

If u hav segs for 4 gigs then they probably have 'segment descriptors' as 0x08 and 0x10 -- 0x08 for code and 0x10 for data in the GDT(Global Descriptor Table) with the first entry in the gdt a null entry. So to jump to the newly loaded kernel you need to compute the actual physical memory address... e.g. In rmode if it is 0x3000:0x0000 That comes out to be 0x30000 . So to jump you use jmp 0x08:0x30000 (0x08 is the descriptor)
Or just replace jmp by call to issue a far call. Yes you can use retf.
kubeos
Member
Member
Posts: 138
Joined: Tue Jan 30, 2007 2:31 pm
Location: Kamloops BC, CANADA
Contact:

user programs or modules

Post by kubeos »

Hi,

Hi, sorry, I meant to say that my kernel is already loaded and running in pmode, and I want to be able to jump to a user program that the kernel loads from disk.. thanks for your answers, they were what I was looking for.
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post by Dex »

If your GDT descriptor are 0 based, you could run a simple program by loading test.bin to 0x400000 and in your kernel doing this
call 0x400000
; the program will return here

The test.bin example

Code: Select all

use32                           ; we want 32bit addressing
        ORG   0x400000          ; where our program is loaded to
                                ; put some more code here
        ret                     ; return to kernel
NOTE: You would also need to enable A20 to load other 1mb.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: user programs or modules

Post by Brendan »

Hi,
kubeos wrote:Hi, sorry, I meant to say that my kernel is already loaded and running in pmode, and I want to be able to jump to a user program that the kernel loads from disk.. thanks for your answers, they were what I was looking for.
If the user program is meant to run at CPL=3 (so that it can't trash the kernel for e.g.) then you can't jump to it's code from the kernel. Instead you need to "return" to it's code - i.e. push the relevant values on the kernel's stack, then either do "RETF" or "IRETD".

This will load CS, EIP, SS and ESP (and EFLAGS for IRETD) from the stack, and switch from CPL=0 to CPL=3 at the same time.


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.
Post Reply