Skip to contentSkip to conversation

Experimental initial patch providing accelerated OpenGL for Linux i386 (2nd attempt to post)

PATCH New Even Rouault 1 participant 1 message Nov 16, 2006 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Experimental initial patch providing accelerated OpenGL for Linux i386 (2nd attempt to post)

Here's an attempt of providing accelerated OpenGL for QEMU.
I must underline first that it should really really be considered as experimental or a proof-of-concept. There's
still a lot of work to do to make it reliable and releasable.
So what is it exactly ?
- a patch for QEMU itself that intercepts any call to 'int $0x99' which is the interface I have chosen to dedicate to
  communication between guest user programs and QEMU. The modification of QEMU existing code is very slight.
  The essential of code on the guest side is in new files.
- a replacement libGL.so library that must be used inside the Linux guest. The library redirects guest OpenGL and GLX calls
  to host side, where your host libGL.so will do the real job.

It works currently only with Linux x86 as guest and host OS.

Now, the good news : which programs do currently work with this patch ?
- glxinfo
- glxgears. I get ~330 FPS with QEMU/GL (direct GLX), ~210 FPS through a SSH console on the guest OS (indirect GLX)
- some mesa demos programs.
    * tunnel     : ~26 FPS with QEMU/GL,  ~3 FPS through a SSH console on the guest OS
    * teapot     : ~50 FPS with QEMU/GL,  ~7 FPS through a SSH console on the guest OS
    * fire       : ~14 FPS with QEMU/GL,  ~2 FPS through a SSH console on the guest OS
    * arbfplight : ~100 FPS with QEMU/GL,  not possible through a SSH console on the guest OS (GL_ARB_vertex_program not available)
- ... and ppracer ! (some crash from time to time. Starting at ~20 FPS at the beginning of the race, and finishing at ~10 FPS... strange...)

How fast is it ? Enough fast to have 15 FPS in ppracer with KQEMU disabled. When KQEMU is enabled, paradoxically, the performance is really poor :-(

Successfully tested on the following platforms :
- Ubuntu Edgy x86 as host with ATI FGLRX driver / FC5 x86 as guest
- Ubuntu Dapper x86 as host with NVIDIA Proprietary driver / Ubuntu Dapper x86 as guest

Which programs do not work : probably most for the moment ;-) I'd like to make it work with XGL (I've downloaded the latest Mandriva 2007 Live CD and I'm quite
impatient to enable the 3D in it) and GoogleEarth.

How to use it ? (ONLY on Linux i386 and with only i386-softmmu as target-list)
* Apply the patch and recompile QEMU
* Lauch QEMU with -enable-gl option
* Get the compiled libGL.so from ./i386-softmmu and copy it to the guest OS
* In the guest OS : LD_LIBRARY_PATH=/path/to/replacement/libGL.so glxgears

TODO list :
- implement correctly full OpenGL API
- improve vertex arrays support
- maybe fix how the end of the guest process is detected
- make replacement libGL work when compiled with optimization flags
- integrate in a better way the window that popups on the guest side with the main qemu window
- integrate it properly into QEMU build system (helper_opengl.c can't compile with -O2 : 'unable to find a register to spill in class `DIREG'')
- understand why it's slower when KQEMU enabled and try to fix that if possible
- enable several guest OS processes to use OpenGL at the same time
- make it run on x86_64, and allowing any combination guest x86/x86_64 and host x86/x86_64, other archs
- port it to other UNIX-like OS with X
- improve security if possible (preventing malicious guest code from crashing host qemu)
- clean the code !
- much testing and debugging
- port it to Windows platform (first OpenGL / WGL, then D3D through Wine libs ?)
- make a patch to Valgrind to make it happy with 'int $0x99' when running on guest side
- ...

The new files :
- opengl_client.c  : the OpenGL guest library
- helper_opengl.c : main guest-side code
- gl_func.h, server_stub.c, client_stub.c : generated files by parse_gl_h.c from gl.h parsing
- glgetv_cst.h  : generated file from parsing of a man page
- gl_func_perso.h, opengl_func.h : hand-written "prototypes"

In the mean time, enjoy !

dyngen-exec.h +4 -0
diff -Nrup --exclude=CVS --exclude='*~' --exclude='config-host*' --exclude=Makefile --exclude=config.mak --exclude=config.h qemu.ori/dyngen-exec.h qemu/dyngen-exec.h
@@ -75,12 +75,16 @@ typedef signed long long int64_t;
 #define UINT32_MAX		(4294967295U)
 #define UINT64_MAX		((uint64_t)(18446744073709551615))
 
+#ifndef _STDIO_H
+
 typedef struct FILE FILE;
 extern int fprintf(FILE *, const char *, ...);
 extern int printf(const char *, ...);
 #undef NULL
 #define NULL 0
 
+#endif
+
 #ifdef __i386__
 #define AREG0 "ebp"
 #define AREG1 "ebx"
kqemu.c +30 -2
diff -Nrup --exclude=CVS --exclude='*~' --exclude='config-host*' --exclude=Makefile --exclude=config.mak --exclude=config.h qemu.ori/kqemu.c qemu/kqemu.c
@@ -87,6 +87,7 @@ unsigned long *modified_ram_pages;
 unsigned int nb_modified_ram_pages;
 uint8_t *modified_ram_pages_table;
 extern uint32_t **l1_phys_map;
+extern int enable_gl;
 
 #define cpuid(index, eax, ebx, ecx, edx) \
   asm volatile ("cpuid" \
@@ -721,6 +722,7 @@ int kqemu_cpu_exec(CPUState *env)
     ret = ioctl(kqemu_fd, KQEMU_EXEC, kenv);
 #endif
 #endif
+
     if (env->cpuid_features & CPUID_FXSR)
         save_native_fp_fxsave(env);
     else
@@ -749,7 +751,6 @@ int kqemu_cpu_exec(CPUState *env)
     env->kernelgsbase = kenv->kernelgsbase;
 #endif
 #endif
-
     /* flush pages as indicated by kqemu */
     if (kenv->nb_pages_to_flush >= KQEMU_FLUSH_ALL) {
         tlb_flush(env, 1);
@@ -825,13 +826,40 @@ int kqemu_cpu_exec(CPUState *env)
     else
         env->hflags &= ~HF_OSFXSR_MASK;
         
+    /* OPENGL Stuff */
+    if (enable_gl && ((env->hflags & HF_CPL_MASK) == 3))
+    {
+      int index = (env->eip >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
+      if (env->segs[R_CS].base != 0)
+      {
+        fprintf(stderr, "env->segs[R_CS].base != 0 !\n");
+      }
+      else if (__builtin_expect(env->tlb_table[1][index].addr_code != 
+                           (env->eip & TARGET_PAGE_MASK), 0))
+      {
+        ldub_code(env->eip);
+      }
+      if (env->tlb_table[1][index].addend)
+      {
+        unsigned char* ptr = env->eip + env->tlb_table[1][index].addend;
+        if (ptr[0] == 0xCD && ptr[1] == 0x99)
+        {
+          helper_opengl(env);
+        }
+        /*else if (ptr[0] == 0xCD && ptr[1] == 0x80)
+        {
+          fprintf(stderr, "system call %d\n", env->regs[R_EAX]);
+        }*/
+      }
+    }
+    
 #ifdef DEBUG
     if (loglevel & CPU_LOG_INT) {
         fprintf(logfile, "kqemu: kqemu_cpu_exec: ret=0x%x\n", ret);
     }
 #endif
     if (ret == KQEMU_RET_SYSCALL) {
-        /* syscall instruction */
+  /* syscall instruction */
         return do_syscall(env, kenv);
     } else 
     if ((ret & 0xff00) == KQEMU_RET_INT) {
Makefile.target +24 -4
diff -Nrup --exclude=CVS --exclude='*~' --exclude='config-host*' --exclude=Makefile --exclude=config.mak --exclude=config.h qemu.ori/Makefile.target qemu/Makefile.target
@@ -285,8 +285,6 @@ ifdef CONFIG_GDBSTUB
 OBJS+=gdbstub.o
 endif
 
-all: $(PROGS)
-
 $(QEMU_USER): $(OBJS)
 	$(CC) $(LDFLAGS) -o $@ $^  $(LIBS)
 ifeq ($(ARCH),alpha)
@@ -435,8 +433,28 @@ ifdef CONFIG_WIN32
 SDL_LIBS := $(filter-out -mwindows, $(SDL_LIBS)) -mconsole
 endif
 
+ifeq ($(TARGET_BASE_ARCH), i386)
+parse_gl_h: parse_gl_h.c
+	$(CC) -o $@ $<
+server_stub.c: parse_gl_h
+	./parse_gl_h
+client_stub.c: parse_gl_h
+	./parse_gl_h
+gl_func.h: parse_gl_h
+	./parse_gl_h
+# -O2 doesn't work --> 'unable to find a register to spill in class `DIREG''
+GL_CFLAGS := -Wall -g -fno-strict-aliasing
+helper_opengl.o: helper_opengl.c server_stub.c gl_func.h
+	$(CC) $(GL_CFLAGS) $(DEFINES) -c -o $@ $<
+LIBOBJS+=helper_opengl.o
+libGL.so: client_stub.c gl_func.h
+	$(CC) $(GL_CFLAGS) $(SRC_PATH)/target-i386/opengl_client.c -shared -o libGL.so -I. -I$(SRC_PATH)/target-i386
+PROGS+=libGL.so
+GL_GENERATED_FILES=libGL.so parse_gl_h server_stub.c client_stub.c gl_func.h
+endif
+
 $(QEMU_SYSTEM): $(VL_OBJS) libqemu.a
-	$(CC) $(VL_LDFLAGS) -o $@ $^ $(LIBS) $(SDL_LIBS) $(COCOA_LIBS) $(VL_LIBS)
+	$(CC) $(VL_LDFLAGS) -o $@ $^ $(LIBS) $(SDL_LIBS) $(COCOA_LIBS) $(VL_LIBS) -lGL
 
 cocoa.o: cocoa.m
 	$(CC) $(CFLAGS) $(DEFINES) -c -o $@ $<
@@ -545,7 +563,7 @@ $(OBJS) $(LIBOBJS) $(VL_OBJS): config.h 
 	$(CC) $(DEFINES) -c -o $@ $<
 
 clean:
-	rm -f *.o  *.a *~ $(PROGS) gen-op.h opc.h op.h nwfpe/*.o slirp/*.o fpu/*.o
+	rm -f *.o  *.a *~ $(PROGS) gen-op.h opc.h op.h nwfpe/*.o slirp/*.o fpu/*.o $(GL_GENERATED_FILES)
 
 install: all 
 ifneq ($(PROGS),)
@@ -561,3 +579,5 @@ audio.o sdlaudio.o dsoundaudio.o ossaudi
 fmodaudio.o alsaaudio.o mixeng.o sb16.o es1370.o gus.o adlib.o: \
 CFLAGS := $(CFLAGS) -Wall -Werror -W -Wsign-compare
 endif
+
+all: $(PROGS)
target-i386/gl_func_perso.h +93 -0

93 lines not on this page · raw message

target-i386/glgetv_cst.h +250 -0

250 lines not on this page · raw message

target-i386/helper.c +10 -0
diff -Nrup --exclude=CVS --exclude='*~' --exclude='config-host*' --exclude=Makefile --exclude=config.mak --exclude=config.h qemu.ori/target-i386/helper.c qemu/target-i386/helper.c
@@ -3782,6 +3782,16 @@ void update_fp_status(void)
 #endif
 }
 
+void helper_opengl(CPUState *env);
+
+void helper_int99()
+{
+  regs_to_env();
+  helper_opengl(env);
+  env_to_regs();
+}
+
+
 #if !defined(CONFIG_USER_ONLY) 
 
 #define MMUSUFFIX _mmu
target-i386/helper_opengl.c +1764 -0

1,764 lines not on this page · raw message

Les fichiers binaires qemu.ori/target-i386/libGL.so.1 et qemu/target-i386/libGL.so.1 sont différents.

target-i386/op.c +7 -0
diff -Nrup --exclude=CVS --exclude='*~' --exclude='config-host*' --exclude=Makefile --exclude=config.mak --exclude=config.h qemu.ori/target-i386/op.c qemu/target-i386/op.c
@@ -132,6 +132,13 @@ static inline target_long lshift(target_
 
 #endif
 
+void helper_int99(void);
+
+void OPPROTO op_int99(void)
+{
+  helper_int99();
+}
+
 /* operations with flags */
 
 /* update flags with T0 and T1 (add/sub case) */
target-i386/opengl_client.c +1956 -0

736 lines not on this page · raw message

target-i386/opengl_func.h +424 -0

424 lines not on this page · raw message

target-i386/parse_gl_h.c +645 -0

645 lines not on this page · raw message

target-i386/translate.c +9 -0
diff -Nrup --exclude=CVS --exclude='*~' --exclude='config-host*' --exclude=Makefile --exclude=config.mak --exclude=config.h qemu.ori/target-i386/translate.c qemu/target-i386/translate.c

Lines of this file are not shown. View the raw message.

vl.c +21 -3
diff -Nrup --exclude=CVS --exclude='*~' --exclude='config-host*' --exclude=Makefile --exclude=config.mak --exclude=config.h qemu.ori/vl.c qemu/vl.c

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Part of the diff is not shown here. View the raw message.