How to map a specific C++ file as user code?

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

How to map a specific C++ file as user code?

Post by Octacone »

I need to have a single C++ file mapped as user code and I can't figure out how to do it. I can't just map the whole kernel as user accessible, that would be stupidly dangerous. I just want this one object file and all of its data to be mapped as user code/data. I could load it as a separate module but I can't since I don't have any kernel libraries yet and even if I had, I would still have to access some kernel structures. So I was thinking maybe I should somehow obtain the address of that code and map it, but it is kind of impossible since we're dealing with C++ objects. So I guess there must be a linker flag or something that would allow me to specify a compiled object file and put it on a separate page?
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: How to map a specific C++ file as user code?

Post by iansjack »

If it’s user code then it shouldn’t be part of the kernel, and it shouldn’t be able to access kernel objects (other than through system calls. I think the problem may be your design.
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: How to map a specific C++ file as user code?

Post by kzinti »

That sounds similar to Linux's VSO.

I do something like this for my system calls. I basically put the code in a ".vdso" section. It is mapped in kernel space as it is part of the kernel. But then I also map this code at the end of user space. You just have to make sure this code is relocatable as it won't have the same address in user and kernel space.

Now this code is never actually called in kernel space. It is meant for user space to provide the functionality required to make system calls. I also plan to add functions to read the current time and monotonic timers without having to make a system call. Obviously this code can't reference any variable / function elsewhere in the kernel.

I wrote my user space code in assembly. I am not sure you can do this (easily) for C++ code and objects. What kind of functionality are you tried to map to user space?
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: How to map a specific C++ file as user code?

Post by Octocontrabass »

Octacone wrote:So I guess there must be a linker flag or something that would allow me to specify a compiled object file and put it on a separate page?
You can tell the linker to treat one object file differently from the rest.

But it won't be able to access any symbols that are elsewhere in your kernel, since the rest of your kernel will be inaccessible from ring 3, so I'm not sure if this is really the best way to do what you want.
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: How to map a specific C++ file as user code?

Post by Octacone »

Sorry, I should have been more clear. I'm adding virtual 8086 mode support which runs in user mode by default, which means I can't leave my VBE driver mapped as kernel, since it would generate a PF.
I can enter it just fine and the environment is set up properly I just can't execute any code because it has to be mapped as user.
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
Post Reply