Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic userspace #228

Merged
merged 21 commits into from
Sep 24, 2021
Merged

Add basic userspace #228

merged 21 commits into from
Sep 24, 2021

Conversation

vinc
Copy link
Owner

@vinc vinc commented Aug 5, 2021

This PR is an experiment into implementing a userspace mode.

I started from this article: https://nfil.dev/kernel/rust/coding/rust-kernel-to-userspace-and-back/
And the OSDev wiki as usual: https://wiki.osdev.org/Getting_to_Ring_3
Then I found this gem: https://github.com/WartaPoirier-corp/ananos/blob/dev/docs/notes/context-switch.md
Along with the associated code in the repo that is a perfect starting point to get an idea of how to get something working.

@vinc
Copy link
Owner Author

vinc commented Aug 5, 2021

To produce the binary of an example program to run in userspace, first create a file prog.asm with something like this:

bits 64
start:
  mov rax, 0x0
  mov rdi, 0x3ff0000000000000
  mov rsi, 0x0
  mov rdx, 0x0
  int 0x80
  jmp start

Then run the following command:

nasm -f bin prog.asm -o prog.bin && xxd -i prog.bin

And you will get this byte array:

  0xb8, 0x00, 0x00, 0x00, 0x00, 0x48, 0xbf, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0xf0, 0x3f, 0xbe, 0x00, 0x00, 0x00, 0x00, 0xba, 0x00, 0x00, 0x00,
  0x00, 0xcd, 0x80, 0xeb, 0xe3

In MOROS this program will call sleep(1.0) through a syscall repetitively.

@vinc
Copy link
Owner Author

vinc commented Aug 6, 2021

We can use include_bytes!("../dsk/bin/sleep") to skip the manual conversion into a byte array. And since we have a filesytem the next step is to read from the disk at runtime.

@vinc
Copy link
Owner Author

vinc commented Aug 6, 2021

Userspace \o/
userspace

@vinc
Copy link
Owner Author

vinc commented Aug 7, 2021

The debug outputs from the above screenshot are now replaced by a program /bin/test that send a number to the syscall TEST who print it, increment it, and send it back. The program will also increment what it receive, sleep 1 second, then start again.

This should suffice to show communication between userspace and the kernel.

@vinc
Copy link
Owner Author

vinc commented Sep 18, 2021

The string in the hello world binary is at address 32 in the code but accessing it result in a page fault:

page-fault

@vinc
Copy link
Owner Author

vinc commented Sep 18, 2021

The address from a user code segment just needed to be translated by the kernel to fix the exception above.

@vinc vinc changed the title Testing userspace code Add basic userspace Sep 24, 2021
@vinc
Copy link
Owner Author

vinc commented Sep 24, 2021

Next step will be to add an ELF loader.

@vinc vinc marked this pull request as ready for review September 24, 2021 08:05
@vinc vinc merged commit 07ff769 into trunk Sep 24, 2021
@vinc vinc deleted the feature/userspace branch September 24, 2021 16:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant