Bug 59672 - Add -m16 support for x86
Summary: Add -m16 support for x86
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 4.9.0
: P3 enhancement
Target Milestone: 4.9.0
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-01-03 23:00 UTC by H. Peter Anvin
Modified: 2015-09-29 20:22 UTC (History)
9 users (show)

See Also:
Host:
Target: i386-*-*
Build:
Known to work:
Known to fail:
Last reconfirmed: 2014-01-04 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description H. Peter Anvin 2014-01-03 23:00:27 UTC
It would be a very good thing to have an official way to generate 16-bit binaries on x86 (i386), even if all that is provided is a command-line flag that issues a .code16gcc assembly directive first in the generated assembly file.

In the Linux kernel, we currently have to do a bunch of hacks to make sure that gcc doesn't issue any instructions *before* the .code16gcc directive:

                   $(call cc-option, -fno-toplevel-reorder,\
                   $(call cc-option, -fno-unit-at-a-time)) \
                   $(call cc-option, -fno-stack-protector) \

... and the list is likely to grow, which is why providing an actually supported compiler flag for this would be desirable.
Comment 1 Andrew Pinski 2014-01-03 23:03:49 UTC
Note GCC does not even support real 16bit code for x86.  So pretending GCC's output is 16bit code is a joke.

Why can't you just write the 16bit binary support in assembly for the kernel?
Comment 2 H. Peter Anvin 2014-01-03 23:09:49 UTC
It is much cleaner to have it in C.  We converted the assembly code to C back in 2007 and it has been much easier to maintain ever since.  It works fine, thankyouverymuch; it isn't *optimal* 16-bit code, but it is real and valid 16-bit code and we use it as such.

Sure, optimization would be nice.  Do we care?  Not a lot.
Comment 3 Steven Bosscher 2014-01-04 23:29:51 UTC
Nice idea. Not likely to be implemented any time soon, because it's quite complicated to get right.

Takes more than just placing a directive right. With the right set of flags the compiler would still emit 32bit instructions. Actually disabling 32bits instructions is a lot more work: add a TARGET_16BITS or an attribute_enabled to almost all patterns in i386.md and friends. Not helpful for the vast majority of users, and kernel people will complain more about the inevitable slowdown (and, as usual, point out the general incompetence of gcc hackers :-)

(Why/when is 16bit code still necessary anyway? Before entering protected mode?)
Comment 4 H. Peter Anvin 2014-01-04 23:32:44 UTC
You are missing the plain fact that *it is already working*.

.code16gcc is a binutils directive which takes 32-bit code emitted by gcc and assembles it to produce valid (although suboptimal) 16-bit code.  So it really *is* just a matter of putting the directive in the right place.

And yes, it is used before entering protected mode.
Comment 5 David Woodhouse 2014-01-22 20:47:24 UTC
Note that LLVM/clang has a -m16 option now which does the same thing. Again, not needing dirty hacks to ensure that asm(".code16gcc") really *is* the first thing the assembler sees.
Comment 6 H. Peter Anvin 2014-01-22 22:31:32 UTC
This could also be implemented in binutils as a --code16gcc option, in which case gcc users would have to use "-m32 -Wa,--code16gcc".  Ugly but would work.
Comment 7 H.J. Lu 2014-01-23 12:52:11 UTC
I put the initial -m16 support on hjl/x86/m16 branch at:

http://gcc.gnu.org/git/?p=gcc.git;a=summary

We need to add some run-time testcases and fix any bugs.
Comment 8 hjl@gcc.gnu.org 2014-01-28 16:23:17 UTC
Author: hjl
Date: Tue Jan 28 16:22:45 2014
New Revision: 207196

URL: http://gcc.gnu.org/viewcvs?rev=207196&root=gcc&view=rev
Log:
Add -m16 support for x86

The .code16gcc directive was added to binutils back in 1999:

---
   '.code16gcc' provides experimental support for generating 16-bit code
from gcc, and differs from '.code16' in that 'call', 'ret', 'enter',
'leave', 'push', 'pop', 'pusha', 'popa', 'pushf', and 'popf'
instructions default to 32-bit size.  This is so that the stack pointer
is manipulated in the same way over function calls, allowing access to
function parameters at the same stack offsets as in 32-bit mode.
'.code16gcc' also automatically adds address size prefixes where
necessary to use the 32-bit addressing modes that gcc generates.
---

It encodes 32-bit assembly instructions generated by GCC in 16-bit format
so that GCC can be used to generate 16-bit instructions.  To do that, the
.code16gcc directive must be placed at the very beginning of the assembly
code.  This patch adds -m16 to x86 backend by:

1. Add -m16 and make it mutually exclusive with -m32, -m64 and -mx32.
2. Treat -m16 like -m32 so that --32 is passed to assembler.
3. Output .code16gcc at the very beginning of the assembly code.
4. Turn off 64-bit ISA when -m16 is used.

	PR target/59672
	* config/i386/gnu-user64.h (SPEC_32): Add "m16|" to "m32".
	(SPEC_X32): Likewise.
	(SPEC_64): Likewise.
	* config/i386/i386.c (ix86_option_override_internal): Turn off
	OPTION_MASK_ISA_64BIT, OPTION_MASK_ABI_X32 and OPTION_MASK_ABI_64
	for TARGET_16BIT.
	(x86_file_start): Output .code16gcc for TARGET_16BIT.
	* config/i386/i386.h (TARGET_16BIT): New macro.
	(TARGET_16BIT_P): Likewise.
	* config/i386/i386.opt: Add m16.
	* doc/invoke.texi: Document -m16.

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/config/i386/gnu-user64.h
    trunk/gcc/config/i386/i386.c
    trunk/gcc/config/i386/i386.h
    trunk/gcc/config/i386/i386.opt
    trunk/gcc/doc/invoke.texi
Comment 9 David Woodhouse 2014-01-29 00:16:49 UTC
Thanks. This appears to work for me to build the Linux kernel's 16-bit boot code with the patch at
http://lkml.kernel.org/r/1389180083-23249-3-git-send-email-David.Woodhouse@intel.com
Comment 10 felix 2015-09-29 20:10:20 UTC
Out of curiosity: given that hjl's -m16 patch has been merged, is there a reason for keeping this open? Is anyone hoping to have a more complete support for x86-16 target, or at least to drop the .code16gcc kludge?
Comment 11 H.J. Lu 2015-09-29 20:22:42 UTC
Fixed for 4.9.0.