kaniini / libucontext

ucontext implementation featuring glibc-compatible ABI
Other
102 stars 41 forks source link

PowerPC: regs member in uc_mcontext #36

Closed nmeum closed 2 years ago

nmeum commented 2 years ago

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 commented 2 years ago

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

kaniini commented 2 years ago

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

nmeum commented 2 years ago

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 commented 2 years ago

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

nmeum commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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.