<div dir="ltr">I am reminded of the ms_abi / sysv_abi attribute annotations. Would it be possible to leverage those to insert the appropriate spills and fills? So, anything annotated ms_abi is presumably an entry point into the SysV side of things, so any explicitly ms_abi annotated function would add X18 as a CSR.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Apr 26, 2019 at 5:30 AM Martin Storsjö via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi,<br>
<br>
When using Wine to run Windows ARM64 executables on Linux, there's one <br>
major ABI incompatibility between the two; Windows treats the x18 <br>
register as the reserved platform register, while it is free to be <br>
clobbered anywhere in code on Linux.<br>
<br>
The Wine code sets up this register before passing control over to the <br>
Windows executable code, but whenever the Windows code calls a function <br>
implemented in Wine, this register ends up clobbered.<br>
<br>
The initial solution is to compile Wine itself with the -ffixed-x18 flag, <br>
to treat the x18 register as reserved within Wine, so that no compiler <br>
generated code touches it. This works fine as long as Wine makes no calls <br>
to other libraries from the outside Linux distribution - but e.g. as soon <br>
as any glibc function is called, it can end up clobbered again.<br>
<br>
The full, proper solution would of course to be rebuilding one's Linux <br>
distribution from scratch with -ffixed-x18 on every single library. But <br>
this is of course pretty much impractical.<br>
<br>
My current makeshift workaround for the matter is to reroute calls from <br>
native Windows code to Wine functions via a relay wrapper, which backs up <br>
and restores the register. This works in practice, but hasn't been <br>
accepted upstream, as it is not deemed a correct/complete solution. Also, <br>
if the emulated Windows API functions do callbacks, the register would <br>
need to be restored before the callbacks call windows code.<br>
<br>
Another idea which was raised in <br>
<a href="https://bugs.winehq.org/show_bug.cgi?id=38780#c13" rel="noreferrer" target="_blank">https://bugs.winehq.org/show_bug.cgi?id=38780#c13</a>, which disucsses this <br>
matter, was if it would be possible to make Clang/LLVM always back up and <br>
restore x18 on any call to any other function, to make sure that the <br>
register maintains the right value as long as running in Wine code. (One <br>
concern is that this could end up rather expensive though.)<br>
<br>
I didn't really find any straighforward way of starting to implement this <br>
however. Does anyone here happen to have ideas about how one could either <br>
implement this, or solve it in another way?<br>
<br>
// Martin<br>
<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>