Skip to contentSkip to conversation

qemu on mips patch

Qemu-devel Jun Peng 2 participants 2 messages Jun 2, 2007 – Jun 3, 2007 Kbuild

lore
Share & tools
lore

Jun Peng

author
#0 lore raw

Hello!

Are you are just testing host_mips qemu? I have been doing the same work two or three month

ago on a mips + linux system. But, it have some bugs however.

It is for qemu 0.6.1 refer to qemu-irix.patch at first. And yesterday, I made a qemu 0.9.0

patch. It seems working(only target-i386). In order to handle relocation symbol, I writed a

little tool named op_mips.c. This tool is the key of my patch.

host detail:
cpu: loognson 2E(mips compatible, little endian)
os: debian linux 4.0
kernel: linux 2.6.18
gcc: 3.4.6
glibc: 2.3.6

test:
in full system mode: can run windows 98 or linux(x86) in user mode: can run some i386 elf programs except wine!

bugs:
In user mode, it catchs a SIGSEGV when launching a windows PE program. I have been trying to

solve this problem a long time. But unfortunately, I failed.

I wondered if you could give me some help about qemu + wine. And hope new qemu release

supply host_mips soon.

There is the patch: qemu-0.9.0-loongson.patch

Thanks.
Peng Jun

cpu-all.h +16 -1
diff -Nurb qemu-0.9.0/cpu-all.h qemu-loongson/cpu-all.h
@@ -20,7 +20,7 @@
 #ifndef CPU_ALL_H
 #define CPU_ALL_H
 
-#if defined(__arm__) || defined(__sparc__)
+#if defined(__arm__) || defined(__sparc__) || defined(__mips__)
 #define WORDS_ALIGNED
 #endif
 
@@ -1001,6 +1001,21 @@
         return rval.i64;
 #endif
 }
+
+#elif defined(__mips__)
+
+#include <time.h>
+static inline int64_t cpu_get_real_ticks (void)
+{
+    struct timespec t;
+    // linux/time.h
+#ifndef	CLOCK_MONOTONIC
+#define	CLOCK_MONOTONIC	1
+#endif
+    clock_gettime(CLOCK_MONOTONIC, &t);
+    return t.tv_sec*1000000000LL+t.tv_nsec;
+}
+
 #else
 /* The host CPU doesn't have an easily accessible cycle counter.
    Just return a monotonically increasing vlue.  This will be totally wrong,
cpu-exec.c +19 -0
diff -Nurb qemu-0.9.0/cpu-exec.c qemu-loongson/cpu-exec.c
@@ -1478,6 +1478,25 @@
                              &uc->uc_sigmask, puc);
 }
 
+#elif defined(__mips__)
+
+#include <sys/user.h>
+int cpu_signal_handler(int host_signum, void *pinfo, 
+                       void *puc)
+{
+    siginfo_t *info = pinfo;
+    struct ucontext *uc = puc;
+    unsigned long pc;
+    int is_write;
+    
+    pc = uc->uc_mcontext.gregs[EF_REG31];	// ???
+    /* XXX: compute is_write */
+    is_write = 0;
+    return handle_cpu_signal(pc, (unsigned long)info->si_addr, 
+                             is_write,
+                             &uc->uc_sigmask, puc);
+}
+
 #else
 
 #error host CPU specific signal handler needed
dyngen.c +83 -1

113 lines not on this page · raw message

dyngen-exec.h +21 -0
diff -Nurb qemu-0.9.0/dyngen-exec.h qemu-loongson/dyngen-exec.h
@@ -227,9 +227,21 @@
 #else
 extern int __op_param1, __op_param2, __op_param3;
 #endif
+#if defined(__mips__)
+/* On MIPS, parameters to a c expression are passed via the global pointer.
+ * We don't want that. */
+#define PARAMN(index) ({ register int _r; \
+		asm("lui %0,%%hi(__op_param" #index ")\n\t" \
+			"ori %0,%0,%%lo(__op_param" #index ")" \
+			: "=r"(_r)); _r; })
+#define PARAM1 PARAMN(1)
+#define PARAM2 PARAMN(2)
+#define PARAM3 PARAMN(3)
+#else
 #define PARAM1 ((long)(&__op_param1))
 #define PARAM2 ((long)(&__op_param2))
 #define PARAM3 ((long)(&__op_param3))
+#endif
 #endif /* !defined(__alpha__) */
 
 extern int __op_jmp0, __op_jmp1, __op_jmp2, __op_jmp3;
@@ -275,5 +287,14 @@
 #ifdef __mc68000
 #define EXIT_TB() asm volatile ("rts")
 #endif
+#ifdef __mips__
+#define EXIT_TB() asm volatile ("j	$31\n\tnop")
+//#define GOTO_LABEL_PARAM(n) asm volatile ("j	" ASM_NAME(__op_gen_label) #n)
+#define	GOTO_LABEL_PARAM(n)	\
+	asm volatile ("lui	$25,%hi(__op_gen_label" #n")\n\
+	ori	$25,$25,%lo(__op_gen_label" #n")\n\
+	j	$25\n\
+	nop")
+#endif
 
 #endif /* !defined(__DYNGEN_EXEC_H__) */
dyngen.h +8 -0
diff -Nurb qemu-0.9.0/dyngen.h qemu-loongson/dyngen.h
@@ -120,6 +120,14 @@
 }
 #endif
 
+#ifdef __mips__
+#include <sys/cachectl.h>
+static inline void flush_icache_range(unsigned long start, unsigned long stop)
+{
+    cacheflush((void*)start,stop-start,BCACHE);
+}
+#endif
+
 #ifdef __alpha__
 
 register int gp asm("$29");
exec-all.h +19 -0
diff -Nurb qemu-0.9.0/exec-all.h qemu-loongson/exec-all.h
@@ -481,6 +481,25 @@
 }
 #endif
 
+#ifdef __mips__
+static inline int testandset (int *spinlock)
+{
+        int ret;
+        __asm__ __volatile__(".set mips2\n"
+                "ll %0,%1\n"
+                ".set noreorder\n"
+                "bnez %0,1f\n"
+                "li %0,0\n" /* branch delay slot */
+                "li %0,1\n"
+                "sc %0,%1\n"
+                "1:\n"
+                ".set reorder\n"
+                : "=r" (ret), "=m" (*spinlock));
+
+        return !ret;
+}
+#endif
+
 typedef int spinlock_t;
 
 #define SPIN_LOCK_UNLOCKED 0
linux-user/mmap.c +2 -2
diff -Nurb qemu-0.9.0/linux-user/mmap.c qemu-loongson/linux-user/mmap.c
@@ -157,7 +157,7 @@
     target_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len;
     long host_start;
 #if defined(__alpha__) || defined(__sparc__) || defined(__x86_64__) || \
-    defined(__ia64)
+    defined(__ia64) || defined(__mips__)
     static target_ulong last_start = 0x40000000;
 #elif defined(__CYGWIN__)
     /* Cygwin doesn't have a whole lot of address space.  */
@@ -202,7 +202,7 @@
 
     if (!(flags & MAP_FIXED)) {
 #if defined(__alpha__) || defined(__sparc__) || defined(__x86_64__) || \
-    defined(__ia64) || defined(__CYGWIN__)
+    defined(__ia64) || defined(__CYGWIN__) || defined(__mips__)
         /* tell the kenel to search at the same place as i386 */
         if (real_start == 0) {
             real_start = last_start;
linux-user/syscall.c +24 -5
diff -Nurb qemu-0.9.0/linux-user/syscall.c qemu-loongson/linux-user/syscall.c
@@ -58,7 +58,13 @@
 #define ltchars host_ltchars /* same as target */
 
 #include <linux/termios.h>
+#ifdef	__mips__
+#define	__KERNEL__
 #include <linux/unistd.h>
+#undef	__KERNEL__
+#else
+#include <linux/unistd.h>
+#endif
 #include <linux/utsname.h>
 #include <linux/cdrom.h>
 #include <linux/hdreg.h>
@@ -68,6 +74,10 @@
 
 #include "qemu.h"
 
+// testing!!! only for host-mips target-i386
+#include "x86_sys_errno.c"
+#include "x86_sys_socket.c"
+
 //#define DEBUG
 
 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_SPARC) \
@@ -176,7 +186,8 @@
 static inline long get_errno(long ret)
 {
     if (ret == -1)
-        return -errno;
+//        return -errno;
+	    return -gs_x86_errno(errno);
     else
         return ret;
 }
@@ -445,6 +456,8 @@
         }
 
         cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
+	if(cmsg->cmsg_level == X86_SOL_SOCKET)
+		cmsg->cmsg_level = SOL_SOCKET;
         cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
         cmsg->cmsg_len = CMSG_LEN(len);
 
@@ -489,10 +502,13 @@
         }
 
         target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
+	if(cmsg->cmsg_level == SOL_SOCKET)
+		cmsg->cmsg_level = X86_SOL_SOCKET;
         target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
         target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
 
-        if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
+	if (cmsg->cmsg_level != X86_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
+
             gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
             memcpy(target_data, data, len);
         } else {
@@ -752,6 +768,7 @@
 
 static long do_socket(int domain, int type, int protocol)
 {
+	return get_errno(x86_sys_socket(domain, type, protocol));
 #if defined(TARGET_MIPS)
     switch(type) {
     case TARGET_SOCK_DGRAM:
@@ -1086,7 +1103,8 @@
             target_ulong optval = tgetl(vptr + 3 * n);
             socklen_t optlen = tgetl(vptr + 4 * n);
 
-            ret = do_setsockopt(sockfd, level, optname, optval, optlen);
+//            ret = do_setsockopt(sockfd, level, optname, optval, optlen);
+	    ret = get_errno(x86_sys_setsockopt(sockfd, level, optname, (void*)optval, optlen));
         }
         break;
     case SOCKOP_getsockopt:
@@ -1097,7 +1115,8 @@
             target_ulong optval = tgetl(vptr + 3 * n);
             target_ulong poptlen = tgetl(vptr + 4 * n);
 
-            ret = do_getsockopt(sockfd, level, optname, optval, poptlen);
+//            ret = do_getsockopt(sockfd, level, optname, optval, poptlen);
+	    ret = get_errno(x86_sys_getsockopt(sockfd, level, optname, (void*)optval, (void*)poptlen));
         }
         break;
     default:
@@ -3951,7 +3970,7 @@
 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_set_thread_area) || defined(TARGET_NR_getdomainname)
     unimplemented_nowarn:
 #endif
-        ret = -ENOSYS;
+	ret = -gs_x86_errno(ENOSYS);
         break;
     }
  fail:
linux-user/x86_sys_errno.c +411 -0

411 lines not on this page · raw message

linux-user/x86_sys_socket.c +189 -0

189 lines not on this page · raw message

Makefile +14 -1
diff -Nurb qemu-0.9.0/Makefile qemu-loongson/Makefile
@@ -34,9 +34,16 @@
 
 all: $(TOOLS) $(DOCS) recurse-all
 
+ifeq ($(ARCH),mips)
+subdir-%: dyngen$(EXESUF) op_mips$(EXESUF)
+	$(MAKE) -C $(subst subdir-,,$@) all
+
+else
 subdir-%: dyngen$(EXESUF)
 	$(MAKE) -C $(subst subdir-,,$@) all
 
+endif
+
 recurse-all: $(patsubst %,subdir-%, $(TARGET_DIRS))
 
 qemu-img$(EXESUF): qemu-img.c cutils.c block.c block-raw.c block-cow.c block-qcow.c aes.c block-vmdk.c block-cloop.c block-dmg.c block-bochs.c block-vpc.c block-vvfat.c block-qcow2.c
@@ -45,10 +52,16 @@
 dyngen$(EXESUF): dyngen.c
 	$(HOST_CC) $(CFLAGS) $(CPPFLAGS) $(BASE_CFLAGS) -o $@ $^
 
+ifeq ($(ARCH),mips)
+op_mips$(EXESUF): op_mips.c
+	$(HOST_CC) $(CFLAGS) $(DEFINES) -o $@ $^
+
+endif
+
 clean:
 # avoid old build problems by removing potentially incorrect old files
 	rm -f config.mak config.h op-i386.h opc-i386.h gen-op-i386.h op-arm.h opc-arm.h gen-op-arm.h 
-	rm -f *.o *.a $(TOOLS) dyngen$(EXESUF) TAGS *.pod *~ */*~
+	rm -f *.o *.a $(TOOLS) dyngen$(EXESUF) op_mips$(EXESUB) TAGS *.pod *~ */*~
 	$(MAKE) -C tests clean
 	for d in $(TARGET_DIRS); do \
 	$(MAKE) -C $$d $@ || exit 1 ; \
Makefile.target +16 -1
diff -Nurb qemu-0.9.0/Makefile.target qemu-loongson/Makefile.target
@@ -27,6 +27,7 @@
 LIBS=
 HELPER_CFLAGS=$(CFLAGS)
 DYNGEN=../dyngen$(EXESUF)
+OP_MIPS=../op_mips$(EXESUF)
 # user emulator name
 TARGET_ARCH2=$(TARGET_ARCH)
 ifeq ($(TARGET_ARCH),arm)
@@ -160,7 +161,8 @@
 endif
 
 ifeq ($(ARCH),mips)
-BASE_LDFLAGS+=-Wl,-T,$(SRC_PATH)/$(ARCH).ld
+OP_CFLAGS+=-fno-delayed-branch
+BASE_LDFLAGS+=-Wl,-T,$(SRC_PATH)/$(ARCH).ld -lrt
 endif
 
 ifeq ($(HAVE_GCC3_OPTIONS),yes)
@@ -504,9 +506,22 @@
 gen-op.h: op.o $(DYNGEN)
 	$(DYNGEN) -g -o $@ $<
 
+ifeq ($(ARCH),mips)
+op.o: op2.s
+	$(CC) $(OP_CFLAGS) $(CPPFLAGS) -c -o $@ op2.s
+
+op2.s:	op.s
+	$(OP_MIPS) < op.s > op2.s
+
+op.s:	op.c
+	$(CC) $(OP_CFLAGS) $(CPPFLAGS) -S -o op.s $<
+
+else
 op.o: op.c
 	$(CC) $(OP_CFLAGS) $(CPPFLAGS) -c -o $@ $<
 
+endif
+
 # HELPER_CFLAGS is used for all the code compiled with static register
 # variables
 ifeq ($(TARGET_BASE_ARCH), i386)
mips.ld +238 -0

238 lines not on this page · raw message

op_mips.c +160 -0

160 lines not on this page · raw message

slirp/mbuf.h +7 -1
diff -Nurb qemu-0.9.0/slirp/mbuf.h qemu-loongson/slirp/mbuf.h
@@ -39,6 +39,12 @@
 
 #define m_freem m_free
 
+#if defined(__mips__)
+#define WORDS_ALIGNED
+#define ATTRIBUTE_ALIGNED __attribute__((aligned (4)))
+#else
+#define ATTRIBUTE_ALIGNED
+#endif
 
 #define MINCSIZE 4096	/* Amount to increase mbuf if too small */
 
@@ -70,7 +76,7 @@
 	int	mh_size;		/* Size of data */
 	struct	socket *mh_so;
 	
-	caddr_t	mh_data;		/* Location of data */
+	caddr_t	mh_data ATTRIBUTE_ALIGNED;		/* Location of data */
 	int	mh_len;			/* Amount of data in this mbuf */
 };
 
#1 lore raw
Quoted 7 lines · Jun Peng

Hello!

Are you are just testing host_mips qemu? I have been doing the same work two or three month

ago on a mips + linux system. But, it have some bugs however.

Quoted 7 lines

It is for qemu 0.6.1 refer to qemu-irix.patch at first. And yesterday, I made a qemu 0.9.0

patch. It seems working(only target-i386). In order to handle relocation symbol, I writed a

little tool named op_mips.c. This tool is the key of my patch.

In the meanwhile the latest CVS qemu has preliminary mips host support, could you refresh you patch to the latest version? I think the current CVS version does the right thing without relying on on an equivalent to your op_mips.c tool.

Also, a patch for the errno handling on mips went just in, please write your version for x86 in the same style, and split it out in a separate patch.

Thiemo