1 /* 2 * gemu main 3 * 4 * Copyright (c) 2003 Fabrice Bellard 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 */ 20 #include <stdlib.h> 21 #include <stdio.h> 22 #include <stdarg.h> 23 #include <string.h> 24 #include <errno.h> 25 #include <unistd.h> 26 27 #include "gemu.h" 28 29 #include "cpu-i386.h" 30 31 #define DEBUG_LOGFILE "/tmp/gemu.log" 32 33 FILE *logfile = NULL; 34 int loglevel; 35 36 unsigned long x86_stack_size; 37 unsigned long stktop; 38 39 void gemu_log(const char *fmt, ...) 40 { 41 va_list ap; 42 43 va_start(ap, fmt); 44 vfprintf(stderr, fmt, ap); 45 va_end(ap); 46 } 47 48 /***********************************************************/ 49 /* CPUX86 core interface */ 50 51 void cpu_x86_outb(int addr, int val) 52 { 53 fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val); 54 } 55 56 void cpu_x86_outw(int addr, int val) 57 { 58 fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val); 59 } 60 61 void cpu_x86_outl(int addr, int val) 62 { 63 fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val); 64 } 65 66 int cpu_x86_inb(int addr) 67 { 68 fprintf(stderr, "inb: port=0x%04x\n", addr); 69 return 0; 70 } 71 72 int cpu_x86_inw(int addr) 73 { 74 fprintf(stderr, "inw: port=0x%04x\n", addr); 75 return 0; 76 } 77 78 int cpu_x86_inl(int addr) 79 { 80 fprintf(stderr, "inl: port=0x%04x\n", addr); 81 return 0; 82 } 83 84 void write_dt(void *ptr, unsigned long addr, unsigned long limit, 85 int seg32_bit) 86 { 87 unsigned int e1, e2, limit_in_pages; 88 limit_in_pages = 0; 89 if (limit > 0xffff) { 90 limit = limit >> 12; 91 limit_in_pages = 1; 92 } 93 e1 = (addr << 16) | (limit & 0xffff); 94 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000); 95 e2 |= limit_in_pages << 23; /* byte granularity */ 96 e2 |= seg32_bit << 22; /* 32 bit segment */ 97 stl((uint8_t *)ptr, e1); 98 stl((uint8_t *)ptr + 4, e2); 99 } 100 101 uint64_t gdt_table[6]; 102 103 void cpu_loop(struct CPUX86State *env) 104 { 105 for(;;) { 106 int err; 107 uint8_t *pc; 108 109 err = cpu_x86_exec(env); 110 pc = env->seg_cache[R_CS].base + env->eip; 111 switch(err) { 112 case EXCP0D_GPF: 113 if (pc[0] == 0xcd && pc[1] == 0x80) { 114 /* syscall */ 115 env->eip += 2; 116 env->regs[R_EAX] = do_syscall(env, 117 env->regs[R_EAX], 118 env->regs[R_EBX], 119 env->regs[R_ECX], 120 env->regs[R_EDX], 121 env->regs[R_ESI], 122 env->regs[R_EDI], 123 env->regs[R_EBP]); 124 } else { 125 goto trap_error; 126 } 127 break; 128 default: 129 trap_error: 130 fprintf(stderr, "0x%08lx: Unknown exception %d, aborting\n", 131 (long)pc, err); 132 abort(); 133 } 134 process_pending_signals(env); 135 } 136 } 137 138 void usage(void) 139 { 140 printf("gemu version " GEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n" 141 "usage: gemu [-d] program [arguments...]\n" 142 "Linux x86 emulator\n" 143 ); 144 exit(1); 145 } 146 147 int main(int argc, char **argv) 148 { 149 const char *filename; 150 struct target_pt_regs regs1, *regs = ®s1; 151 struct image_info info1, *info = &info1; 152 CPUX86State *env; 153 int optind; 154 155 if (argc <= 1) 156 usage(); 157 loglevel = 0; 158 optind = 1; 159 if (argv[optind] && !strcmp(argv[optind], "-d")) { 160 loglevel = 1; 161 optind++; 162 } 163 filename = argv[optind]; 164 165 /* init debug */ 166 if (loglevel) { 167 logfile = fopen(DEBUG_LOGFILE, "w"); 168 if (!logfile) { 169 perror(DEBUG_LOGFILE); 170 exit(1); 171 } 172 setvbuf(logfile, NULL, _IOLBF, 0); 173 } 174 175 /* Zero out regs */ 176 memset(regs, 0, sizeof(struct target_pt_regs)); 177 178 /* Zero out image_info */ 179 memset(info, 0, sizeof(struct image_info)); 180 181 if(elf_exec(filename, argv+optind, environ, regs, info) != 0) { 182 printf("Error loading %s\n", filename); 183 exit(1); 184 } 185 186 if (loglevel) { 187 fprintf(logfile, "start_brk 0x%08lx\n" , info->start_brk); 188 fprintf(logfile, "end_code 0x%08lx\n" , info->end_code); 189 fprintf(logfile, "start_code 0x%08lx\n" , info->start_code); 190 fprintf(logfile, "end_data 0x%08lx\n" , info->end_data); 191 fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack); 192 fprintf(logfile, "brk 0x%08lx\n" , info->brk); 193 fprintf(logfile, "esp 0x%08lx\n" , regs->esp); 194 fprintf(logfile, "eip 0x%08lx\n" , regs->eip); 195 } 196 197 target_set_brk((char *)info->brk); 198 syscall_init(); 199 signal_init(); 200 201 env = cpu_x86_init(); 202 203 /* linux register setup */ 204 env->regs[R_EAX] = regs->eax; 205 env->regs[R_EBX] = regs->ebx; 206 env->regs[R_ECX] = regs->ecx; 207 env->regs[R_EDX] = regs->edx; 208 env->regs[R_ESI] = regs->esi; 209 env->regs[R_EDI] = regs->edi; 210 env->regs[R_EBP] = regs->ebp; 211 env->regs[R_ESP] = regs->esp; 212 env->eip = regs->eip; 213 214 /* linux segment setup */ 215 env->gdt.base = (void *)gdt_table; 216 env->gdt.limit = sizeof(gdt_table) - 1; 217 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xffffffff, 1); 218 write_dt(&gdt_table[__USER_DS >> 3], 0, 0xffffffff, 1); 219 cpu_x86_load_seg(env, R_CS, __USER_CS); 220 cpu_x86_load_seg(env, R_DS, __USER_DS); 221 cpu_x86_load_seg(env, R_ES, __USER_DS); 222 cpu_x86_load_seg(env, R_SS, __USER_DS); 223 cpu_x86_load_seg(env, R_FS, __USER_DS); 224 cpu_x86_load_seg(env, R_GS, __USER_DS); 225 226 cpu_loop(env); 227 /* never exits */ 228 return 0; 229 } 230