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

PowerPC: regs member in uc_mcontext #36

Closed
nmeum opened this issue Dec 19, 2021 · 7 comments
Closed

PowerPC: regs member in uc_mcontext #36

nmeum opened this issue Dec 19, 2021 · 7 comments

Comments

@nmeum
Copy link
Contributor

nmeum commented Dec 19, 2021

I am currently using libcontext with gcc-go, which uses ucontext functions to implement goroutines. In this regard, I am trying to reduce the amount of gcc-go patches required to make it work on musl systems. On the PowerPC architecture, libgo from gcc-go assumes a regs member to be present in the machine-specific representation of the saved context (uc_mcontext):

  1. https://github.com/gcc-mirror/gcc/blob/7e5aeda340d71a84fbd15504e848a949b2a00d5a/libgo/runtime/go-signal.c#L227
  2. https://github.com/gcc-mirror/gcc/blob/7e5aeda340d71a84fbd15504e848a949b2a00d5a/libgo/runtime/go-signal.c#L345-L350

Unfortunately, this regs member does not seem to be available on libucontext, thus requiring patching gcc-go. However, libucontext seems to expose the same registers via the gp_regs member. For example, uc_mcontext.regs->nip seems to be accessible with libucontext as uc_mcontext.gp_regs[REG_NIP].

I am not an expert on ucontext and it is presently unclear to me where the uc_mcontext fields are standardized (since POSIX does not seem to mandate those) but I am wondering if it would be possible to add the regs member to uc_mcontext on PowerPC to make existing code (i.e. gcc-go) work without additional patches.

@kaniini
Copy link
Owner

kaniini commented Dec 19, 2021

On musl, libucontext uses the musl headers for structure definitions. So we should probably ask dalias.

@kaniini
Copy link
Owner

kaniini commented Dec 19, 2021

(I would assume the issue is that _GNU_SOURCE needs to be defined though)

@nmeum
Copy link
Contributor Author

nmeum commented Dec 19, 2021

On musl, libucontext uses the musl headers for structure definitions. So we should probably ask dalias.

https://git.musl-libc.org/cgit/musl/commit/?id=c2518a8efb6507f1b41c3b12e03b06f8f2317a1f

I suppose libucontext could provide a definition of struct pt_regs on PowerPC then?

@kaniini
Copy link
Owner

kaniini commented Dec 21, 2021

I think pt_regs should come from kernel, but i’m not sure what header has it.

@nmeum
Copy link
Contributor Author

nmeum commented Dec 25, 2021

Seems to be defined in asm/ptrace.h: https://github.com/torvalds/linux/blob/a3314262eede9c909a0c797f16f25f941d12c78d/arch/powerpc/include/asm/ptrace.h#L28

Would it be possible to include this Linux header from ucontext.h on PowerPC or what would be the best approach to make the pt_regs definition visible automatically when using libucontext on PowerPC?

@kaniini
Copy link
Owner

kaniini commented Dec 25, 2021

As the headers from musl are used on musl, I don’t have any control over them. You could include asm/ptrace.h in the GCC-Go side of things though.

@nmeum
Copy link
Contributor Author

nmeum commented Dec 26, 2021

I looked into how glibc handles this. Similar to musl, glibc also seem to declare pt_regs as an incomplete type on PowerPC [0]. So even with glibc, gcc-go would need an include of asm/ptrace.h on PowerPC. However, it seems that some other glibc headers (like sys/user.h) cause an implicit include of asm/ptrace.h which is why the code linked in the issue description probably compiles on glibc [1]. I guess this is a "bug" a gcc-go then, closing this.

@nmeum nmeum closed this as completed Dec 26, 2021
kaniini added a commit that referenced this issue Mar 24, 2022
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

No branches or pull requests

2 participants