Skip to contentSkip to conversation

kqemu does not build on hybrid x86_64/i386 system

Qemu-devel Chris Dukes 2 participants 3 messages Oct 29, 2008 – Nov 7, 2008 Kbuild

lore
Share & tools
lore
#0 lore raw

Hi,
I'm attempting to build kqemu 1.4.0~pre1 on a debian sid i386 system running debian's 2.6.26-1-amd64 kernel.

It fails attemping to assemble nexus_asm.S with a combination of x86_64/nexus_asm.S:63: Error: bad register name `%rax'
type errors
and
x86_64/nexus_asm.S:84: Error: `movzwq' is only supported in 64-bit mode

I believe this is happening because the kqemu script makes extensive use of uname -m to determine the architecture for build and incorrectly assumes that just because it's x86_64ish that gcc will emit 64bit code.

After much kludging (And it's incorrect for anything except Linux), I was able to have common/Makefile apply -m32 or -m64 as I thought was appropriate. Now the .S files assemble fine, but link fails with x86_64/monitor_asm.o: In function `__monitor_exception':
(.text+0x68d): undefined reference to KQEMU_STATE_monitor_ss_null_sel' x86_64/monitor_asm.o: In function exception_return':
(.text+0x6d3): undefined reference to KQEMU_STATE_monitor_ss_null_sel' x86_64/monitor_asm.o: In function exception_return16':
(.text+0x776): undefined reference to KQEMU_STATE_monitor_cs32_sel' x86_64/monitor_asm.o: In function __monitor_interrupt':
(.text+0x81c): undefined reference to `KQEMU_STATE_monitor_ss_null_sel'

It's somewhat confusing as it builds fine for an x86_64 kernel with x86_64 userspace and 686 kernel with i386 userspace.

Attached are the full logs, as well as my kludgery.

Signature · 1 line
Chris Dukes

./configure --kernel-path=/lib/modules/2.6.26-1-amd64/build

Source path       /usr/src/modules/kqemu
C compiler        gcc
Host C compiler   gcc
make              make
host CPU          x86_64

kernel sources /lib/modules/2.6.26-1-amd64/build
kbuild type 2.6
/usr/bin/make
make[2]: Entering directory `/usr/src/modules/kqemu'
make -C common all
make[3]: Entering directory `/usr/src/modules/kqemu/common'
gcc -Wall -O2 -Werror -g -D__KERNEL__ -I.. -o genoffsets genoffsets.c
./genoffsets > monitor_def.h
gcc -D__KERNEL__ -nostdinc -iwithprefix include -I. -I.. -D__ASSEMBLY__ -c -o x86_64/nexus_asm.o x86_64/nexus_asm.S x86_64/nexus_asm.S: Assembler messages:
x86_64/nexus_asm.S:42: Error: bad register name `%rbp'
x86_64/nexus_asm.S:43: Error: bad register name `%rsp'
x86_64/nexus_asm.S:44: Error: bad register name `%rbx'
x86_64/nexus_asm.S:45: Error: bad register name `%r12'
x86_64/nexus_asm.S:46: Error: bad register name `%r13'
x86_64/nexus_asm.S:47: Error: bad register name `%r14'
x86_64/nexus_asm.S:48: Error: bad register name `%r15'
x86_64/nexus_asm.S:50: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:51: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:52: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:53: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:54: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:56: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:57: Error: bad register name `%rsp'
x86_64/nexus_asm.S:59: Error: bad register name `%rax'
x86_64/nexus_asm.S:60: Error: bad register name `%rax'
x86_64/nexus_asm.S:61: Error: bad register name `%rax'
x86_64/nexus_asm.S:62: Error: bad register name `%rax'
x86_64/nexus_asm.S:63: Error: bad register name `%rax'
x86_64/nexus_asm.S:64: Error: bad register name `%rax'
x86_64/nexus_asm.S:68: Error: bad register name `%rax'
x86_64/nexus_asm.S:69: Error: bad register name `%rcx'
x86_64/nexus_asm.S:70: Error: bad register name `%rax'
x86_64/nexus_asm.S:71: Error: bad register name `%rax'
x86_64/nexus_asm.S:73: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:74: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:75: Error: bad register name `%rax'
x86_64/nexus_asm.S:76: Error: bad register name `%rcx'
x86_64/nexus_asm.S:78: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:79: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:81: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:84: Error: `movzwq' is only supported in 64-bit mode
x86_64/nexus_asm.S:85: Error: bad register name `%rax'
x86_64/nexus_asm.S:86: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:87: Error: bad register name `%rax'
x86_64/nexus_asm.S:88: Error: suffix or operands invalid for `lret'
x86_64/nexus_asm.S:91: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:93: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:96: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:97: Error: bad register name `%rcx)'
x86_64/nexus_asm.S:98: Error: bad register name `%rcx)'
x86_64/nexus_asm.S:99: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:100: Error: bad register name `%rdx'
x86_64/nexus_asm.S:101: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:102: Error: bad register name `%rdx'
x86_64/nexus_asm.S:104: Error: bad register name `%r12'
x86_64/nexus_asm.S:105: Error: bad register name `%r13'
x86_64/nexus_asm.S:107: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:110: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:111: Error: bad register name `%rax'
x86_64/nexus_asm.S:113: Error: bad register name `%rax'
x86_64/nexus_asm.S:116: Error: bad register name `%r15'
x86_64/nexus_asm.S:117: Error: bad register name `%r14'
x86_64/nexus_asm.S:118: Error: bad register name `%r13'
x86_64/nexus_asm.S:119: Error: bad register name `%r12'
x86_64/nexus_asm.S:120: Error: bad register name `%rbx'
x86_64/nexus_asm.S:121: Error: bad register name `%rbp'
x86_64/nexus_asm.S:129: Error: bad register name `%rbp'
x86_64/nexus_asm.S:130: Error: bad register name `%rsp'
x86_64/nexus_asm.S:131: Error: bad register name `%rbx'
x86_64/nexus_asm.S:132: Error: bad register name `%r12'
x86_64/nexus_asm.S:133: Error: bad register name `%r13'
x86_64/nexus_asm.S:134: Error: bad register name `%r14'
x86_64/nexus_asm.S:135: Error: bad register name `%r15'
x86_64/nexus_asm.S:137: Error: bad register name `%rsp'
x86_64/nexus_asm.S:140: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:141: Error: bad register name `%rax'
x86_64/nexus_asm.S:143: Error: bad register name `%rax'
x86_64/nexus_asm.S:144: Error: bad register name `%rax'
x86_64/nexus_asm.S:147: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:148: Error: bad register name `%rax'
x86_64/nexus_asm.S:149: Error: bad register name `%rax'
x86_64/nexus_asm.S:153: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:154: Error: bad register name `%rcx'
x86_64/nexus_asm.S:155: Error: bad register name `%rax'
x86_64/nexus_asm.S:156: Error: bad register name `%rax'
x86_64/nexus_asm.S:159: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:160: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:161: Error: bad register name `%rax'
x86_64/nexus_asm.S:162: Error: bad register name `%rcx'
x86_64/nexus_asm.S:164: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:165: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:166: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:168: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:171: Error: `movzwq' is only supported in 64-bit mode
x86_64/nexus_asm.S:172: Error: bad register name `%rax'
x86_64/nexus_asm.S:173: Error: monitor2kernel_jmp_offset(%rip)' is not a valid base/index expression x86_64/nexus_asm.S:174: Error: bad register name %rax'
x86_64/nexus_asm.S:175: Error: suffix or operands invalid for `lret'
x86_64/nexus_asm.S:179: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:182: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:183: Error: bad register name `%rcx)'
x86_64/nexus_asm.S:187: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:188: Error: bad register name `%rax'
x86_64/nexus_asm.S:190: Error: bad register name `%rdi)'
x86_64/nexus_asm.S:192: Error: bad register name `%r15'
x86_64/nexus_asm.S:193: Error: bad register name `%r14'
x86_64/nexus_asm.S:194: Error: bad register name `%r13'
x86_64/nexus_asm.S:195: Error: bad register name `%r12'
x86_64/nexus_asm.S:196: Error: bad register name `%rbx'
x86_64/nexus_asm.S:197: Error: bad register name `%rbp'
make[3]: *** [x86_64/nexus_asm.o] Error 1
make[3]: Leaving directory `/usr/src/modules/kqemu/common'
make[2]: *** [kqemu.ko] Error 2
make[2]: Leaving directory `/usr/src/modules/kqemu'
make[1]: *** [binary-modules] Error 2
make[1]: Leaving directory `/usr/src/modules/kqemu'
make: *** [kdist_build] Error 2

./configure --kernel-path=/lib/modules/2.6.26-1-amd64/build --cc=gcc-4.1 --host-cc=gcc-4.1 CONFIG_X86_64=y

Source path       /usr/src/modules/kqemu
C compiler        gcc-4.1
Host C compiler   gcc-4.1
make              make
host CPU          x86_64

kernel sources /lib/modules/2.6.26-1-amd64/build
kbuild type 2.6
/usr/bin/make
make[2]: Entering directory `/usr/src/modules/kqemu'
make -C common all
make[3]: Entering directory `/usr/src/modules/kqemu/common'
gcc -Wall -O2 -Werror -g -D__KERNEL__ -I.. -o genoffsets genoffsets.c
./genoffsets > monitor_def.h
gcc -m64 -march=k8 -D__KERNEL__ -nostdinc -iwithprefix include -I. -I.. -D__ASSEMBLY__ -c -o x86_64/nexus_asm.o x86_64/nexus_asm.S gcc -m64 -march=k8 -D__KERNEL__ -nostdinc -iwithprefix include -I. -I.. -D__ASSEMBLY__ -c -o x86_64/monitor_asm.o x86_64/monitor_asm.S

gcc -m64 -march=k8 -Wall -O2 -fomit-frame-pointer -fno-strict-aliasing -Werror  -mno-red-zone -fno-stack-protector    -fpic  -D__KERNEL__ -nostdinc -iwithprefix include -I. -I.. -c -o monitor.o monitor.c
gcc -m64 -march=k8 -Wall -O2 -fomit-frame-pointer -fno-strict-aliasing -Werror  -mno-red-zone -fno-stack-protector    -fpic  -D__KERNEL__ -nostdinc -iwithprefix include -I. -I.. -c -o monitor-utils.o monitor-utils.c
gcc -m64 -march=k8 -Wall -O2 -fomit-frame-pointer -fno-strict-aliasing -Werror  -mno-red-zone -fno-stack-protector    -fpic  -D__KERNEL__ -nostdinc -iwithprefix include -I. -I.. -c -o interp.o interp.c

ld -m elf_x86_64 -T x86_64/monitor.ld -o monitor-image.out x86_64/nexus_asm.o x86_64/monitor_asm.o monitor.o monitor-utils.o interp.o x86_64/monitor_asm.o: In function `__monitor_exception':
(.text+0x68d): undefined reference to `KQEMU_STATE_monitor_ss_null_sel'
x86_64/monitor_asm.o: In function `exception_return':
(.text+0x6d3): undefined reference to `KQEMU_STATE_monitor_ss_null_sel'
x86_64/monitor_asm.o: In function `exception_return16':
(.text+0x776): undefined reference to `KQEMU_STATE_monitor_cs32_sel'
x86_64/monitor_asm.o: In function `__monitor_interrupt':
(.text+0x81c): undefined reference to `KQEMU_STATE_monitor_ss_null_sel'
make[3]: *** [monitor-image.out] Error 1
make[3]: Leaving directory `/usr/src/modules/kqemu/common'
make[2]: *** [kqemu.ko] Error 2
make[2]: Leaving directory `/usr/src/modules/kqemu'
make[1]: *** [binary-modules] Error 2
make[1]: Leaving directory `/usr/src/modules/kqemu'
make: *** [kdist_build] Error 2

common/Makefile +4 -4
diff -urwp kqemu.orig/common/Makefile kqemu/common/Makefile
@@ -28,8 +28,8 @@ cc-option = $(shell if test -z "`$(1) $(
               /dev/null 2>&1`"; then echo "$(2)"; else echo "$(3)"; fi  ;) 
 
 HOST_CC=gcc
-MON_CC=gcc
-MON_LD=ld
+MON_CC=$(CC)
+MON_LD=ld -m elf_$(ARCH)
 ifdef CONFIG_WIN32
 TARGET=../kqemu-mod-$(ARCH)-win32.o
 CC=i386-mingw32-gcc
@@ -116,10 +116,10 @@ genmon: genmon.c
 	$(HOST_CC) $(TOOLS_CFLAGS) -o $@ $<
 
 %.o: %.c
-	$(CC) $(CFLAGS) $(DEFINES) $(INCLUDES) -c -o $@ $<
+	$(CC) $(ASMMFLAG) $(CFLAGS) $(DEFINES) $(INCLUDES) -c -o $@ $<
 
 %.o: %.S
-	$(CC) $(DEFINES) $(INCLUDES) -D__ASSEMBLY__ -c -o $@ $<
+	$(CC) $(ASMMFLAG) $(DEFINES) $(INCLUDES) -D__ASSEMBLY__ -c -o $@ $<
 
 clean:
 	rm -f *.o *~ monitor-image.h genoffsets genmon monitor_def.h \
config-host.mak +1 -0
diff -urwp kqemu.orig/config-host.mak kqemu/config-host.mak
@@ -13,6 +13,7 @@ CFLAGS=-O2
 LDFLAGS=
 EXESUF=
 ARCH=i386
+ASMMFLAG=-m32
 KERNEL_PATH=/lib/modules/2.6.26-1-686/build
 CONFIG_KBUILD26=yes
 SRC_PATH=/usr/src/modules/kqemu
configure +23 -8
diff -urwp kqemu.orig/configure kqemu/configure
@@ -275,6 +275,19 @@ if test '!' -f "$kernel_path/Makefile" ;
     kqemu="no"
 fi
 
+if test "$kqemu" = "yes" ; then
+    if grep '^CONFIG_X86_64=y$' $kernel_path/.config; then
+        ARCH=x86_64
+        ASMMFLAG="-m64 -march=k8"
+    elif grep '^CONFIG_X86_32=y$' $kernel_path/.config; then
+        ARCH=i386
+        ASMMFLAG="-m32 -march=generic"
+    else
+        echo "This kernel lacks CONFIG_X86_64 and CONFIG_X86_32 - kqemu cannot be built"
+        kqemu="no"
+    fi
+fi
+
 # find build system (2.6 or legacy)
 kbuild26="yes"
 if grep -q "PATCHLEVEL = 4" $kernel_path/Makefile ; then
@@ -325,14 +338,16 @@ echo "STRIP=$strip -s -R .comment -R .no
 echo "CFLAGS=$CFLAGS" >> $config_mak
 echo "LDFLAGS=$LDFLAGS" >> $config_mak
 echo "EXESUF=$EXESUF" >> $config_mak
-if test "$cpu" = "i386" ; then
-  echo "ARCH=i386" >> $config_mak
-elif test "$cpu" = "x86_64" ; then
-  echo "ARCH=x86_64" >> $config_mak
-else
-  echo "Unsupported CPU"
-  exit 1
-fi
+#if test "$cpu" = "i386" ; then
+#  echo "ARCH=i386" >> $config_mak
+#elif test "$cpu" = "x86_64" ; then
+#  echo "ARCH=x86_64" >> $config_mak
+#else
+#  echo "Unsupported CPU"
+#  exit 1
+#fi
+echo "ARCH=$ARCH" >> $config_mak
+echo "ASMMFLAG=$ASMMFLAG" >> $config_mak
 if test "$bigendian" = "yes" ; then
   echo "WORDS_BIGENDIAN=yes" >> $config_mak
 fi
debian/rules +1 -1
diff -urwp kqemu.orig/debian/rules kqemu/debian/rules
@@ -63,7 +63,7 @@ binary-modules: prep-deb-files
 	dh_clean -k
 
 	# Build the module
-	./configure --kernel-path=$(KSRC)
+	./configure --kernel-path=$(KSRC) --cc=$(CC) --host-cc=$(CC)
 	$(MAKE)
 
 	# Install the module
#1 lore raw
Quoted 27 lines · Chris Dukes

Hi,
I'm attempting to build kqemu 1.4.0~pre1 on a debian sid i386 system running debian's 2.6.26-1-amd64 kernel.

It fails attemping to assemble nexus_asm.S with a combination of x86_64/nexus_asm.S:63: Error: bad register name `%rax'
type errors
and
x86_64/nexus_asm.S:84: Error: `movzwq' is only supported in 64-bit mode

I believe this is happening because the kqemu script makes extensive use of uname -m to determine the architecture for build and incorrectly assumes that just because it's x86_64ish that gcc will emit 64bit code.

After much kludging (And it's incorrect for anything except Linux), I was able to have common/Makefile apply -m32 or -m64 as I thought was appropriate. Now the .S files assemble fine, but link fails with x86_64/monitor_asm.o: In function `__monitor_exception':
(.text+0x68d): undefined reference to KQEMU_STATE_monitor_ss_null_sel' x86_64/monitor_asm.o: In function exception_return':
(.text+0x6d3): undefined reference to KQEMU_STATE_monitor_ss_null_sel' x86_64/monitor_asm.o: In function exception_return16':
(.text+0x776): undefined reference to KQEMU_STATE_monitor_cs32_sel' x86_64/monitor_asm.o: In function __monitor_interrupt':
(.text+0x81c): undefined reference to `KQEMU_STATE_monitor_ss_null_sel'

Quoted 2 lines

It's somewhat confusing as it builds fine for an x86_64 kernel with x86_64 userspace and 686 kernel with i386 userspace.

I recently hacked the kqemu's build system for such cross-compilation scenarios. Find my patches attached. The magic configure command I invoked was this:

./configure --kernel-path=... --cpu=i386 --cc="gcc -m32" \
	--extra-ldflags=-melf_i386

Please note that those patches are no real solutions, maybe even cause problems to other OSes. The build system is desperately looking for a rework, specifically for Linux platforms. I think it should be switched completely to kbuild (with included tools, to build the monitor image), and then checked if the other target OSes can be cleanly folded into that or better kept separately.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
Quoted 1 line

From aaa820fa9ec6ff5a8e9dd07abf70f6becc249cd9 Mon Sep 17 00:00:00 2001

From: Jan Kiszka <[email protected]>
Date: Fri, 24 Oct 2008 12:45:44 +0200
Subject: [PATCH] Cleanup build system for Linux

Cleanup root makefile /wrt Linux module building. Add a distclean rule.

---
 Makefile        |   20 +++++++++++---------
 common/Makefile |    2 +-
 2 files changed, 12 insertions(+), 10 deletions(-)
Makefile +11 -9
index 51e0a02..5a2607a 100644
@@ -14,10 +14,8 @@ else
 ifdef CONFIG_KBUILD26
 # 2.6 build
 
-all: kqemu.ko
-
-kqemu.ko:
-	make -C common all
+all:
+	make -C common monitor-image.h
 	make -C $(KERNEL_PATH) M=`pwd` modules
 
 else
@@ -38,7 +36,11 @@ endif # !CONFIG_WIN32
 
 clean:
 	$(MAKE) -C common clean
-	rm -f kqemu.ko *.o *~
+	rm -rf kqemu.ko *.o *~ .kqemu* Module.* modules.order kqemu.mod.c .tmp_versions \
+               common/.kernel* common/*/.kernel*
+
+distclean: clean
+	rm -rf config-host.mak
 
 FILES=configure Makefile README Changelog LICENSE COPYING \
       install.sh kqemu-linux.c kqemu.h \
@@ -89,10 +91,10 @@ kqemu.o: $(kqemu-objs)
 else
 # called from 2.6 kernel kbuild
 
-obj-m:= kqemu.o
-kqemu-objs:= kqemu-linux.o kqemu-mod.o
+EXTRA_AFLAGS := -I $(M)/common
+EXTRA_CFLAGS := -I $(M)
 
-$(obj)/kqemu-mod.o: $(src)/kqemu-mod-$(ARCH).o
-	cp $< $@
+obj-m := kqemu.o
+kqemu-objs := kqemu-linux.o common/kernel.o common/$(ARCH)/kernel_asm.o
 endif
 endif # PATCHLEVEL
common/Makefile +1 -1
index 181c777..9d109ab 100644
@@ -16,7 +16,7 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 #
-include ../config-host.mak
+-include ../config-host.mak
 #CONFIG_WIN32=y
 #ARCH=i386
 #ARCH=x86_64
Signature · 1 line
1.5.6
Quoted 1 line

From 1a62dc7d129714f2bae483e4f68affafecfcaaf8 Mon Sep 17 00:00:00 2001

From: Jan Kiszka <[email protected]>
Date: Tue, 28 Oct 2008 17:14:14 +0100
Subject: [PATCH] Improve cross-compilation

Improve cross-compilation support of the build system.

---
 Makefile        |    2 +-
 common/Makefile |    7 ++++---
 configure       |    1 +
 3 files changed, 6 insertions(+), 4 deletions(-)
Makefile +1 -1
index 5a2607a..b09636c 100644
@@ -16,7 +16,7 @@ ifdef CONFIG_KBUILD26
 
 all:
 	make -C common monitor-image.h
-	make -C $(KERNEL_PATH) M=`pwd` modules
+	make -C $(KERNEL_PATH) ARCH=$(ARCH) M=`pwd` modules
 
 else
 # 2.4 build
common/Makefile +4 -3
index 9d109ab..80b8d84 100644
@@ -28,7 +28,7 @@ cc-option = $(shell if test -z "`$(1) $(2) -S -o /dev/null -xc \
               /dev/null 2>&1`"; then echo "$(2)"; else echo "$(3)"; fi  ;) 
 
 HOST_CC=gcc
-MON_CC=gcc
+MON_CC:=$(CC)
 MON_LD=ld
 ifdef CONFIG_WIN32
 TARGET=../kqemu-mod-$(ARCH)-win32.o
@@ -50,6 +50,7 @@ endif
 CFLAGS=$(COMMON_CFLAGS)
 MON_CFLAGS=$(COMMON_CFLAGS)
 KERNEL_CFLAGS=$(COMMON_CFLAGS)
+MON_LDFLAGS=$(LDFLAGS)
 
 # Disable SSP if GCC supports it
 MON_CFLAGS+=$(call cc-option,$(MON_CC),-fno-stack-protector,)
@@ -75,7 +76,7 @@ kqemu_global_init kqemu_global_delete
 all: $(TARGET)
 
 $(TARGET): kernel.o $(ARCH)/kernel_asm.o
-	$(LD) -r -o $@ kernel.o $(ARCH)/kernel_asm.o
+	$(LD) $(LDFLAGS) -r -o $@ kernel.o $(ARCH)/kernel_asm.o
 
 monitor-image.h: monitor-image.bin genmon
 	./genmon < $< > $@
@@ -84,7 +85,7 @@ monitor-image.bin: monitor-image.out
 	objcopy -O binary $< $@
 
 monitor-image.out: $(ARCH)/nexus_asm.o $(ARCH)/monitor_asm.o monitor.o monitor-utils.o interp.o
-	$(MON_LD) -T $(ARCH)/monitor.ld -o $@ $^
+	$(MON_LD) $(MON_LDFLAGS) -T $(ARCH)/monitor.ld -o $@ $^
 
 $(ARCH)/nexus_asm.o: $(ARCH)/nexus_asm.S monitor_def.h
 
configure +1 -0
index a4a0585..106d301 100755
@@ -248,6 +248,7 @@ echo "  --cross-prefix=PREFIX    use PREFIX for compile tools [$cross_prefix]"
 echo "  --cc=CC                  use C compiler CC [$cc]"
 echo "  --host-cc=CC             use C compiler CC [$host_cc]"
 echo "  --make=MAKE              use specified make [$make]"
+echo "  --cpu=CPU                specify target CPU [host CPU]"
 echo ""
 exit 1
 fi
Signature · 1 line
1.5.6
Quoted 1 line

From 1fe5e27bd3bd772369b23e3c6f9ec30742d4f864 Mon Sep 17 00:00:00 2001

From: Jan Kiszka <[email protected]>
Date: Tue, 28 Oct 2008 17:18:27 +0100
Subject: [PATCH] Add missing CDECLs

Add CDECL to function definitions that are declared with CDECL.

---
 common/kernel.c |   18 +++++++++---------
 1 files changed, 9 insertions(+), 9 deletions(-)
common/kernel.c +9 -9
index 6a88452..ab441d4 100644
@@ -341,7 +341,7 @@ static void kqemu_del_state(struct kqemu_state *s)
     }
 }
 
-struct kqemu_global_state *kqemu_global_init(int max_locked_pages)
+struct kqemu_global_state * CDECL kqemu_global_init(int max_locked_pages)
 {
     struct kqemu_global_state *g;
     
@@ -354,14 +354,14 @@ struct kqemu_global_state *kqemu_global_init(int max_locked_pages)
     return g;
 }
 
-void kqemu_global_delete(struct kqemu_global_state *g)
+void CDECL kqemu_global_delete(struct kqemu_global_state *g)
 {
     /* XXX: free all existing states ? */
     kqemu_vfree(g);
 }
 
-struct kqemu_state *kqemu_init(struct kqemu_init *d, 
-                               struct kqemu_global_state *g)
+struct kqemu_state *CDECL kqemu_init(struct kqemu_init *d,
+                                     struct kqemu_global_state *g)
 {
     struct kqemu_state *s;
     const struct monitor_code_header *m = (void *)monitor_code;
@@ -756,8 +756,8 @@ struct kqemu_state *kqemu_init(struct kqemu_init *d,
     return NULL;
 }
 
-int kqemu_set_phys_mem(struct kqemu_state *s,
-                       const struct kqemu_phys_mem *kphys_mem)
+int CDECL kqemu_set_phys_mem(struct kqemu_state *s,
+                             const struct kqemu_phys_mem *kphys_mem)
 {
     uint64_t start, size, end, addr;
     uint32_t ram_addr, ram_end, *ptr, pd, io_index;
@@ -951,7 +951,7 @@ static void profile_dump(struct kqemu_state *s)
 }
 #endif
 
-void kqemu_delete(struct kqemu_state *s)
+void CDECL kqemu_delete(struct kqemu_state *s)
 {
     uint8_t *s1;
     struct kqemu_ram_page *rp;
@@ -990,7 +990,7 @@ void kqemu_delete(struct kqemu_state *s)
     kqemu_vfree(s1);
 }
 
-struct kqemu_cpu_state *kqemu_get_cpu_state(struct kqemu_state *s)
+struct kqemu_cpu_state * CDECL kqemu_get_cpu_state(struct kqemu_state *s)
 {
     return &s->cpu_state;
 }
@@ -1063,7 +1063,7 @@ static void apic_restore_nmi(struct kqemu_state *s, int lvt_mask)
     }\
 }
 
-long kqemu_exec(struct kqemu_state *s)
+long CDECL kqemu_exec(struct kqemu_state *s)
 {
     const struct monitor_code_header *m = (void *)monitor_code;
     void (*kernel2monitor)(struct kqemu_state *s) = 
Signature · 1 line
1.5.6
#2 lore raw
Quoted 38 lines · Jan Kiszka
Quoted 27 lines · Chris Dukes

Hi,
I'm attempting to build kqemu 1.4.0~pre1 on a debian sid i386 system running debian's 2.6.26-1-amd64 kernel.

It fails attemping to assemble nexus_asm.S with a combination of x86_64/nexus_asm.S:63: Error: bad register name `%rax'
type errors
and
x86_64/nexus_asm.S:84: Error: `movzwq' is only supported in 64-bit mode

I believe this is happening because the kqemu script makes extensive use of uname -m to determine the architecture for build and incorrectly assumes that just because it's x86_64ish that gcc will emit 64bit code.

After much kludging (And it's incorrect for anything except Linux), I was able to have common/Makefile apply -m32 or -m64 as I thought was appropriate. Now the .S files assemble fine, but link fails with x86_64/monitor_asm.o: In function `__monitor_exception':
(.text+0x68d): undefined reference to KQEMU_STATE_monitor_ss_null_sel' x86_64/monitor_asm.o: In function exception_return':
(.text+0x6d3): undefined reference to KQEMU_STATE_monitor_ss_null_sel' x86_64/monitor_asm.o: In function exception_return16':
(.text+0x776): undefined reference to KQEMU_STATE_monitor_cs32_sel' x86_64/monitor_asm.o: In function __monitor_interrupt':
(.text+0x81c): undefined reference to `KQEMU_STATE_monitor_ss_null_sel'

Quoted 2 lines

It's somewhat confusing as it builds fine for an x86_64 kernel with x86_64 userspace and 686 kernel with i386 userspace.

I recently hacked the kqemu's build system for such cross-compilation scenarios. Find my patches attached. The magic configure command I invoked was this:

./configure --kernel-path=... --cpu=i386 --cc="gcc -m32" \
	--extra-ldflags=-melf_i386
Quoted 6 lines

Please note that those patches are no real solutions, maybe even cause problems to other OSes. The build system is desperately looking for a rework, specifically for Linux platforms. I think it should be switched completely to kbuild (with included tools, to build the monitor image), and then checked if the other target OSes can be cleanly folded into that or better kept separately.

And today I actually ran an i386 kqemu.ko that was cross-built on a x86_64 host. It took an additional patch and an extension of the second one to get things working, see attachments. The extended configure line is

./configure --kernel-path=... --cpu=i386 --cc="gcc -m32" \
	--extra-ldflags=-melf_i386 --host-cc="gcc -m32"

But kqemu's way of generating assembler offsets is not cross-build compatible, it only happens to work for x86-64 -> i386, not for i386 -> x86_64 or non-x86 -> x86. Another reason to go for kbuild (though I haven't checked yet if its offset generation works for out of tree modules as well).

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2 ES-OS
Corporate Competence Center Embedded Linux
Quoted 1 line

From f29be375bc123394a703d21b27591d7f168946ff Mon Sep 17 00:00:00 2001

From: Jan Kiszka <[email protected]>
Date: Tue, 28 Oct 2008 17:14:14 +0100
Subject: [PATCH] Improve cross-compilation

Improve cross-compilation support of the build system.

---
 Makefile        |    2 +-
 common/Makefile |    9 +++++----
 configure       |    1 +
 3 files changed, 7 insertions(+), 5 deletions(-)
Makefile +1 -1
index 5a2607a..b09636c 100644
@@ -16,7 +16,7 @@ ifdef CONFIG_KBUILD26
 
 all:
 	make -C common monitor-image.h
-	make -C $(KERNEL_PATH) M=`pwd` modules
+	make -C $(KERNEL_PATH) ARCH=$(ARCH) M=`pwd` modules
 
 else
 # 2.4 build
common/Makefile +5 -4
index 9d109ab..36d83a6 100644
@@ -27,8 +27,8 @@
 cc-option = $(shell if test -z "`$(1) $(2) -S -o /dev/null -xc \
               /dev/null 2>&1`"; then echo "$(2)"; else echo "$(3)"; fi  ;) 
 
-HOST_CC=gcc
-MON_CC=gcc
+HOST_CC:=$(CC)
+MON_CC:=$(CC)
 MON_LD=ld
 ifdef CONFIG_WIN32
 TARGET=../kqemu-mod-$(ARCH)-win32.o
@@ -50,6 +50,7 @@ endif
 CFLAGS=$(COMMON_CFLAGS)
 MON_CFLAGS=$(COMMON_CFLAGS)
 KERNEL_CFLAGS=$(COMMON_CFLAGS)
+MON_LDFLAGS=$(LDFLAGS)
 
 # Disable SSP if GCC supports it
 MON_CFLAGS+=$(call cc-option,$(MON_CC),-fno-stack-protector,)
@@ -75,7 +76,7 @@ kqemu_global_init kqemu_global_delete
 all: $(TARGET)
 
 $(TARGET): kernel.o $(ARCH)/kernel_asm.o
-	$(LD) -r -o $@ kernel.o $(ARCH)/kernel_asm.o
+	$(LD) $(LDFLAGS) -r -o $@ kernel.o $(ARCH)/kernel_asm.o
 
 monitor-image.h: monitor-image.bin genmon
 	./genmon < $< > $@
@@ -84,7 +85,7 @@ monitor-image.bin: monitor-image.out
 	objcopy -O binary $< $@
 
 monitor-image.out: $(ARCH)/nexus_asm.o $(ARCH)/monitor_asm.o monitor.o monitor-utils.o interp.o
-	$(MON_LD) -T $(ARCH)/monitor.ld -o $@ $^
+	$(MON_LD) $(MON_LDFLAGS) -T $(ARCH)/monitor.ld -o $@ $^
 
 $(ARCH)/nexus_asm.o: $(ARCH)/nexus_asm.S monitor_def.h
 
configure +1 -0
index a4a0585..106d301 100755
@@ -248,6 +248,7 @@ echo "  --cross-prefix=PREFIX    use PREFIX for compile tools [$cross_prefix]"
 echo "  --cc=CC                  use C compiler CC [$cc]"
 echo "  --host-cc=CC             use C compiler CC [$host_cc]"
 echo "  --make=MAKE              use specified make [$make]"
+echo "  --cpu=CPU                specify target CPU [host CPU]"
 echo ""
 exit 1
 fi
Signature · 1 line
1.5.6
Quoted 1 line

From 67f80f5e1bfb8fce2fb6768a8caf97d434536a1d Mon Sep 17 00:00:00 2001

From: Jan Kiszka <[email protected]>
Date: Wed, 5 Nov 2008 01:03:26 +0100
Subject: [PATCH] Switch kernel2monitor and monitor2kernel to regparm

Switch kernel2monitor and monitor2kernel to regparm(1) call convention - the default under Linux anyway. This fixes i386 Linux hosts after moving the build system to kbuild.

---
 common/i386/nexus_asm.S |   12 +++++++-----
 common/kernel.c         |    2 +-
 common/kqemu_int.h      |    3 +--
 3 files changed, 9 insertions(+), 8 deletions(-)
common/i386/nexus_asm.S +7 -5
index 0d79f46..bac55fe 100644
@@ -33,7 +33,8 @@ _start:
         .long ASM_NAME(monitor2kernel_jmp_offset) - _start
         .long ASM_NAME(monitor_exec) - _start
 /*
- * Assumptions when entering kernel2monitor:      
+ * Assumptions when entering kernel2monitor:
+ * %rax = kqemu_state
  * %ss, %ds, %es, %cs are 4G flat 32 bit segments
  * IRQ disabled        
  */
@@ -47,8 +48,8 @@ ASM_NAME(kernel2monitor):
         pushl %ds
         pushl %es
 
-        movl 8(%ebp), %ebx
-        
+        movl %eax, %ebx
+
         str  KQEMU_STATE_kernel_tr_sel(%ebx)
         sidt KQEMU_STATE_kernel_idt(%ebx)
         sgdt KQEMU_STATE_kernel_gdt(%ebx)
@@ -125,6 +126,7 @@ ASM_NAME(kernel2monitor_jmp_offset):
 
 /*
  * Assumptions when entering monitor2kernel:
+ * %rax = kqemu_state
  * IRQ disabled
  */
 ASM_NAME(monitor2kernel):
@@ -134,8 +136,8 @@ ASM_NAME(monitor2kernel):
         pushl %esi
         pushl %edi
 
-        movl 8(%ebp), %ebx
-        
+        movl %eax, %ebx
+
         movl %esp, KQEMU_STATE_monitor_esp(%ebx)
 
         /* clear the debug register if needed */
common/kernel.c +1 -1
index 3c11422..8ed04c6 100644
@@ -1066,7 +1066,7 @@ static void apic_restore_nmi(struct kqemu_state *s, int lvt_mask)
 long CDECL kqemu_exec(struct kqemu_state *s)
 {
     const struct monitor_code_header *m = (void *)monitor_code;
-    void (*kernel2monitor)(struct kqemu_state *s) = 
+    void __attribute__((regparm(1))) (*kernel2monitor)(struct kqemu_state *s) =
         (void *)(m->kernel2monitor + s->nexus_kaddr);
     unsigned long *ptr;
     int ret, apic_nmi_mask, cpl;
common/kqemu_int.h +1 -2
index f19f7ca..6f0b67d 100644
@@ -824,8 +824,7 @@ static inline void wrmsrl(unsigned long msr, uint64_t val)
 
 /* nexus_asm.S */
 
-void kernel2monitor(struct kqemu_state *s);
-void monitor2kernel(struct kqemu_state *s);
+void __attribute__((regparm(1))) monitor2kernel(struct kqemu_state *s);
 
 /* kernel_asm.S */
 void __attribute__((regparm(1))) exec_irq(int intno);
Signature · 1 line
1.5.6