Page 1 of 1

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

Posted: Sun Oct 02, 2022 8:34 am
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?

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

Posted: Sun Oct 02, 2022 8:55 am
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.

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

Posted: Sun Oct 02, 2022 10:34 am
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?

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

Posted: Sun Oct 02, 2022 11:00 am
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.

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

Posted: Sun Oct 02, 2022 1:32 pm
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.