<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Oops, forgot to reply all...</div><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">Charles Davis</b> <span dir="ltr"><<a href="mailto:cdavis5x@gmail.com">cdavis5x@gmail.com</a>></span><br>Date: Tue, Aug 7, 2018 at 3:45 PM<br>Subject: Re: [cfe-dev] 32-bit pointers and calls from 64-bit code<br>To: John McCall <<a href="mailto:rjmccall@apple.com">rjmccall@apple.com</a>><br><br><br><div dir="ltr"><div style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">On Tue, Aug 7, 2018 at 3:11 PM, John McCall <span dir="ltr"><<a href="mailto:rjmccall@apple.com" target="_blank">rjmccall@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div><div><div class="m_-5493704836354310026h5"><blockquote type="cite"><div>On Aug 7, 2018, at 4:06 PM, Charles Davis <<a href="mailto:cdavis5x@gmail.com" target="_blank">cdavis5x@gmail.com</a>> wrote:</div><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Aug 6, 2018 at 7:39 PM, John McCall <span dir="ltr"><<a href="mailto:rjmccall@apple.com" target="_blank">rjmccall@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><span><br><div><br><blockquote type="cite"><div>On Aug 6, 2018, at 6:34 PM, John McCall via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:</div><br class="m_-5493704836354310026m_6771692173135550927m_-8410662625807811171m_-2761031161913787442Apple-interchange-newline"><div><div style="word-wrap:break-word;line-break:after-white-space"><br><div><br><blockquote type="cite"><div>On Aug 6, 2018, at 5:49 PM, Charles Davis via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:</div><br class="m_-5493704836354310026m_6771692173135550927m_-8410662625807811171m_-2761031161913787442Apple-interchange-newline"><div><div dir="ltr"><div style="font-family:arial,helvetica,sans-serif">Resending to the new mailing list address...</div><div style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">Charles Davis</b> <span dir="ltr"><<a href="mailto:cdavis5x@gmail.com" target="_blank">cdavis5x@gmail.com</a>></span><br>Date: Thu, Aug 2, 2018 at 5:59 PM<br>Subject: 32-bit pointers and calls from 64-bit code<br>To: Clang Developers List <<a href="mailto:cfe-dev@cs.uiuc.edu" target="_blank">cfe-dev@cs.uiuc.edu</a>><br><br><br><div dir="ltr"><div><div style="font-family:arial,helvetica,sans-serif;font-size:small;text-decoration-style:initial;text-decoration-color:initial">Hello,</div><div style="font-family:arial,helvetica,sans-serif;font-size:small;text-decoration-style:initial;text-decoration-color:initial"><br></div><div style="font-family:arial,helvetica,sans-serif;font-size:small;text-decoration-style:initial;text-decoration-color:initial">With Apple's impending 32-bit deprecation, my new employer is having a bit of an existential crisis. We think we've found a way forward, but we need a little help from the compiler to accomplish this.</div></div></div></div></div></div></blockquote><div><br></div>This is a pretty big project to be throwing together like this, but good luck.</div><div><br><blockquote type="cite"><div><div dir="ltr"><div class="gmail_quote"><div dir="ltr"><div><div style="font-family:arial,helvetica,sans-serif;font-size:small;text-decoration-style:initial;text-decoration-color:initial">Basically, what we want to do is have code that is technically x86-64 but that can use 32-bit pointers and that can make calls to 32-bit code.</div></div></div></div></div></div></blockquote><div><br></div><div>Sure, this is a relatively well-understood problem.  The most well-known modern example is Linux's support for "x32".</div><br><blockquote type="cite"><div><div dir="ltr"><div class="gmail_quote"><div dir="ltr"><div><div style="font-family:arial,helvetica,sans-serif;font-size:small;text-decoration-style:initial;text-decoration-color:initial">In a separate thread on llvmdev, I asked about LLVM changes we need for this. Here are the corresponding C language extensions we need:</div><div style="text-decoration-style:initial;text-decoration-color:initial"><ul><li><font face="arial, helvetica, sans-serif">(Possibly) a preprocessor macro to be defined when we're building 64-bit code to be called by 32-bit code. This code, while still technically being 64-bit, needs to look and act like 32-bit code to our clients--so we'd like to define </font><font face="monospace, monospace">__i386__</font><font face="arial, helvetica, sans-serif"> et al. as though we were building real 32-bit code.</font></li></ul></div></div></div></div></div></div></blockquote><div>__i386__ generally means, well, the 32-bit Intel target; muddying this by pretending that your variant target is i386 seems like a bad decision.   Can you ask your clients to switch to __LP64__ or some other portable pointer-size macro instead?</div><blockquote type="cite"><div><div dir="ltr"><div class="gmail_quote"><div dir="ltr"><div><div style="text-decoration-style:initial;text-decoration-color:initial"><ul><li><font face="arial, helvetica, sans-serif">Microsoft's </font><font face="monospace, monospace">__ptr32</font><font face="arial, helvetica, sans-serif"> and </font><font face="monospace, monospace">__ptr64</font><font face="arial, helvetica, sans-serif"> keywords--to distinguish 32-bit and 64-bit pointers.</font></li></ul></div></div></div></div></div></div></blockquote><div>I don't know if we actually support those keywords, but conceptually they're just address spaces so it's not a problem.</div><blockquote type="cite"><div><div dir="ltr"><div class="gmail_quote"><div dir="ltr"><div><div style="text-decoration-style:initial;text-decoration-color:initial"><ul><li><font face="arial, helvetica, sans-serif">A pragma to choose the default pointer size--with this, we can avoid littering our code with lots of </font><font face="monospace, monospace">__ptr32</font><font face="arial, helvetica, sans-serif"> keywords.</font></li></ul></div></div></div></div></div></div></blockquote>Doable.  You'll want something that works like the ARC auditing pragmas and tries to force the pragma to stay bounded to a single header, I think.<br><blockquote type="cite"><div><div dir="ltr"><div class="gmail_quote"><div dir="ltr"><div><div style="text-decoration-style:initial;text-decoration-color:initial"><ul><li><font face="arial, helvetica, sans-serif">Attributes for various 32-bit calling conventions. We can't use the existing ones, because they are defined not to do anything in 64-bit code. I'll probably define new ones that are just the old ones suffixed with '32' (e.g. </font><font face="monospace, monospace">__attribute__((cdecl32))</font><font face="arial, helvetica, sans-serif">).</font></li></ul></div></div></div></div></div></div></blockquote>Why do you want this?  You're recompiling all of your code in this new mode, and the default x86_64 CC is more efficient than all of the specialized i386 CCs.  Why not just ignore the attributes?</div><div><blockquote type="cite"><div><div dir="ltr"><div class="gmail_quote"><div dir="ltr"><div><div style="text-decoration-style:initial;text-decoration-color:initial"><ul><li><font face="arial, helvetica, sans-serif">An attribute to declare that a function pointer must be called far, and to declare the segment selector to use when calling it. We need this to be able to transition to a 32-bit code segment. I'm currently leaning towards (ab)using Microsoft's </font><font face="monospace, monospace">__based</font><font face="arial, helvetica, sans-serif"> extension (which originally supported something like this, I believe) for this purpose.</font></li></ul></div></div></div></div></div></div></blockquote><div>Are you really messing around with segments, or are you just trying to be able to to distinguish 32-bit from 64-bit function pointers?</div></div></div></div></blockquote><br></div></span><div>Wait, I just put a few things together.  Are you planning to perform a far call to existing, i386-compiled code in the same process?</div></div></blockquote><div><div style="font-family:arial,helvetica,sans-serif;display:inline">Well... Not exactly.</div></div><div><div style="font-family:arial,helvetica,sans-serif;display:inline"><br></div></div><div><div style="font-family:arial,helvetica,sans-serif;display:inline">We plan to use the Hypervisor framework to create a VM with 32-bit and 64-bit code segments. We'll then make the entirety of the host process's memory visible inside the guest--the intent is to reduce the number of expensive VM exits. But inside this VM... yes, we intend to make far calls directly to existing i386 code. (We also intend to get called by existing i386 code.) We're trying not to implement an entire OS for this purpose--we want to use the host OS to support as much as we can, which is why we're mapping the host process memory into the VM's physical memory.</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div>  That is not going to work without operating system support, and I strongly doubt that macOS has that support — probably not today, and especially not after i386 support is removed.</div></div></blockquote><div><div style="font-family:arial,helvetica,sans-serif;display:inline">I thought so too, but it turns out you don't need a call gate to do this. All you need is the 32-bit segment selector--and on most OSes, that almost never changes, since it's an entry in the GDT. We're well aware that macOS won't even have a 32-bit CS in 10.15--that's why we want to use the Hypervisor framework.</div></div></div></div></div></div></blockquote><div><br></div></div></div>I don't know anything about the Hypervisor framework, but what happens if you take an interrupt while in 32-bit code?</div></div></blockquote></div></div><div><div style="font-family:arial,helvetica,sans-serif;display:inline">There shouldn't be any virtual devices to generate external interrupts. Exceptions and syscalls (the other two primary causes of interrupts) will be handled with a VM exit.</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><span class="m_-5493704836354310026HOEnZb"><font color="#888888"><div><br></div><div>John.</div></font></span></div></blockquote></div><br></div><div class="gmail_extra"><div style="font-family:arial,helvetica,sans-serif">Chip</div><br></div></div>
</div><br></div>