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

Tier 1 Support for x86_64-plan9 #7153

Open
nektro opened this issue Nov 18, 2020 · 15 comments
Open

Tier 1 Support for x86_64-plan9 #7153

nektro opened this issue Nov 18, 2020 · 15 comments
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals. enhancement Solving this issue will likely involve adding new logic or components to the codebase. os-plan9
Milestone

Comments

@nektro
Copy link
Contributor

nektro commented Nov 18, 2020

https://9p.io/plan9/

@zigazeljko
Copy link
Contributor

Targeting plan9 might be a bit trickier, since it uses a custom binary format which LLVM does not currently support (see http://man.cat-v.org/plan_9/6/a.out). From what I read, outputting that format should be doable with a custom linker script and --oformat=binary.

@nektro
Copy link
Contributor Author

nektro commented Nov 21, 2020

@zigazeljko is that not the same a.out kinda file that gets output by default of zig cc when you dont specify -o ?

@zigazeljko
Copy link
Contributor

is that not the same a.out kinda file

No. The a.out file that gets output by default is actually in ELF (or MachO) format, and has no relation to the actual a.out format other than its name.

From Wikipedia:

"a.out" remains the default output file name for executables created by certain compilers and linkers when no output name is specified, even though the created files actually are not in the a.out format.

@andrewrk andrewrk added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Nov 30, 2020
@andrewrk andrewrk added this to the 0.9.0 milestone Nov 30, 2020
@nektro nektro changed the title add support for targeting x86_64-plan9 Tier 1 Support for targeting x86_64-plan9 Feb 3, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@g-w1
Copy link
Contributor

g-w1 commented Jun 6, 2021

I am working on this right now for stage2.

@andrewrk andrewrk added enhancement Solving this issue will likely involve adding new logic or components to the codebase. contributor friendly This issue is limited in scope and/or knowledge of Zig internals. and removed proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. labels Jun 6, 2021
@andrewrk andrewrk modified the milestones: 0.10.0, 1.1.0 Jun 6, 2021
@nektro nektro changed the title Tier 1 Support for targeting x86_64-plan9 Tier 1 Support for x86_64-plan9 Sep 2, 2021
@Leimy
Copy link

Leimy commented Feb 25, 2022

Still being worked? Seems extremely interesting!

@g-w1
Copy link
Contributor

g-w1 commented Feb 25, 2022

Yes, I am just waiting for the self hosted compiler to get better at codegen. Once that happens, progress will be really quick and contributor friendly for adding plan 9 stuff to the stdlib. (The linking backend is already in place)

@zigazeljko
Copy link
Contributor

@g-w1 By the way, is the plan9 syscall ABI (i.e. the stuff in os/plan9/x86_64.zig) documented anywhere?

@g-w1
Copy link
Contributor

g-w1 commented Feb 27, 2022

The syscalls are here http://aiju.de/plan_9/plan9-syscalls, and the abi is the same as the c one.

@zigazeljko
Copy link
Contributor

zigazeljko commented Feb 27, 2022

The page you linked describes the API provided by libc. What I'm looking for is the underlying ABI (application binary interface) provided by the kernel -- how the values are passed to/from kernel and what instructions to use to actually perform the system call.

Taking your x86_64 implementation as an example, how did you find out that:

  • syscall number is passed in rbp;
  • individual arguments are passed on stack, with a zero pushed on top;
  • the actual system call is performed via the syscall instruction (as opposed to int 0x40, for example);
  • the result is returned in rax;
  • register rcx gets clobbered.

@g-w1
Copy link
Contributor

g-w1 commented Feb 27, 2022

Reading generated code from the c compiler (syscall abi is the same as c fncall abi)/playing around a LOT. I don't know if there is a manpage for the abi itself.

@TheDevtop
Copy link

Hi, I'm currently using Plan 9 (9front), is there anything I can do to help Plan 9 support along?

@g-w1
Copy link
Contributor

g-w1 commented Dec 5, 2022

Hello!
I believe the best thing would be to compile some code with it (using the standard library functions), find the bugs (there probably are some), reduce the bugs, and then fix them. If you have questions about how the linker works, you can feel free to contact me and I can talk you through it.
I think I had found a bug with the relocations that you will probably find, but I didn't have the time to look into it.
This tool will be your friend when doing this: https://github.com/g-w1/plan9zig/ .

@euclaise
Copy link

euclaise commented Mar 5, 2023

The handling of register clobbering from syscalls in std/os/plan9/x86_64.zig seems to be incomplete.

I've been working on partially porting musl to 9, and I found that it seems 9 makes no guarantees about saving the values of any registers on amd64 syscalls. I had to save all the relevant registers (https://git.sr.ht/~euclaise/cross9/tree/master/item/musl/p9/x86_64/sys9.s), saving only the ones that were saved here still left registers getting clobbered, although it's possible that I'm missing something.

@g-w1
Copy link
Contributor

g-w1 commented Mar 5, 2023

You are probably correct. I have not tested the syscalls extensively because the x86_64 backend codegen is incomplete and was the bottleneck for me (it's been a few months since I last tried, so it may have gotten better).
Feel free to make a pull request fixing this if you want.

@Leimy
Copy link

Leimy commented Mar 5, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals. enhancement Solving this issue will likely involve adding new logic or components to the codebase. os-plan9
Projects
None yet
Development

No branches or pull requests

8 participants