bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1 | /* |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 2 | * gemu main |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 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> |
bellard | 04369ff | 2003-03-20 22:33:23 +0000 | [diff] [blame] | 23 | #include <string.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 24 | #include <errno.h> |
bellard | 0ecfa99 | 2003-03-03 14:32:43 +0000 | [diff] [blame] | 25 | #include <unistd.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 26 | |
| 27 | #include "gemu.h" |
| 28 | |
bellard | 0ecfa99 | 2003-03-03 14:32:43 +0000 | [diff] [blame] | 29 | #include "cpu-i386.h" |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 30 | |
bellard | 586314f | 2003-03-03 15:02:29 +0000 | [diff] [blame] | 31 | #define DEBUG_LOGFILE "/tmp/gemu.log" |
| 32 | |
| 33 | FILE *logfile = NULL; |
| 34 | int loglevel; |
| 35 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 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 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 48 | /***********************************************************/ |
bellard | 0ecfa99 | 2003-03-03 14:32:43 +0000 | [diff] [blame] | 49 | /* CPUX86 core interface */ |
bellard | 367e86e | 2003-03-01 17:13:26 +0000 | [diff] [blame] | 50 | |
bellard | ba1c6e3 | 2003-03-03 11:58:28 +0000 | [diff] [blame] | 51 | void cpu_x86_outb(int addr, int val) |
bellard | 367e86e | 2003-03-01 17:13:26 +0000 | [diff] [blame] | 52 | { |
| 53 | fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val); |
| 54 | } |
| 55 | |
bellard | ba1c6e3 | 2003-03-03 11:58:28 +0000 | [diff] [blame] | 56 | void cpu_x86_outw(int addr, int val) |
bellard | 367e86e | 2003-03-01 17:13:26 +0000 | [diff] [blame] | 57 | { |
| 58 | fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val); |
| 59 | } |
| 60 | |
bellard | ba1c6e3 | 2003-03-03 11:58:28 +0000 | [diff] [blame] | 61 | void cpu_x86_outl(int addr, int val) |
bellard | 367e86e | 2003-03-01 17:13:26 +0000 | [diff] [blame] | 62 | { |
| 63 | fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val); |
| 64 | } |
| 65 | |
bellard | ba1c6e3 | 2003-03-03 11:58:28 +0000 | [diff] [blame] | 66 | int cpu_x86_inb(int addr) |
bellard | 367e86e | 2003-03-01 17:13:26 +0000 | [diff] [blame] | 67 | { |
| 68 | fprintf(stderr, "inb: port=0x%04x\n", addr); |
| 69 | return 0; |
| 70 | } |
| 71 | |
bellard | ba1c6e3 | 2003-03-03 11:58:28 +0000 | [diff] [blame] | 72 | int cpu_x86_inw(int addr) |
bellard | 367e86e | 2003-03-01 17:13:26 +0000 | [diff] [blame] | 73 | { |
| 74 | fprintf(stderr, "inw: port=0x%04x\n", addr); |
| 75 | return 0; |
| 76 | } |
| 77 | |
bellard | ba1c6e3 | 2003-03-03 11:58:28 +0000 | [diff] [blame] | 78 | int cpu_x86_inl(int addr) |
bellard | 367e86e | 2003-03-01 17:13:26 +0000 | [diff] [blame] | 79 | { |
| 80 | fprintf(stderr, "inl: port=0x%04x\n", addr); |
| 81 | return 0; |
| 82 | } |
| 83 | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 84 | /* default linux values for the selectors */ |
| 85 | #define __USER_CS (0x23) |
| 86 | #define __USER_DS (0x2B) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 87 | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 88 | void write_dt(void *ptr, unsigned long addr, unsigned long limit, |
| 89 | int seg32_bit) |
| 90 | { |
| 91 | unsigned int e1, e2, limit_in_pages; |
| 92 | limit_in_pages = 0; |
| 93 | if (limit > 0xffff) { |
| 94 | limit = limit >> 12; |
| 95 | limit_in_pages = 1; |
| 96 | } |
| 97 | e1 = (addr << 16) | (limit & 0xffff); |
| 98 | e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000); |
| 99 | e2 |= limit_in_pages << 23; /* byte granularity */ |
| 100 | e2 |= seg32_bit << 22; /* 32 bit segment */ |
| 101 | stl((uint8_t *)ptr, e1); |
| 102 | stl((uint8_t *)ptr + 4, e2); |
| 103 | } |
| 104 | |
| 105 | uint64_t gdt_table[6]; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 106 | |
| 107 | void usage(void) |
| 108 | { |
bellard | 27362c8 | 2003-03-16 11:29:31 +0000 | [diff] [blame] | 109 | printf("gemu version " GEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n" |
bellard | 586314f | 2003-03-03 15:02:29 +0000 | [diff] [blame] | 110 | "usage: gemu [-d] program [arguments...]\n" |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 111 | "Linux x86 emulator\n" |
| 112 | ); |
| 113 | exit(1); |
| 114 | } |
| 115 | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 116 | |
| 117 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 118 | int main(int argc, char **argv) |
| 119 | { |
| 120 | const char *filename; |
bellard | 01ffc75 | 2003-02-18 23:00:51 +0000 | [diff] [blame] | 121 | struct target_pt_regs regs1, *regs = ®s1; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 122 | struct image_info info1, *info = &info1; |
bellard | 0ecfa99 | 2003-03-03 14:32:43 +0000 | [diff] [blame] | 123 | CPUX86State *env; |
bellard | 586314f | 2003-03-03 15:02:29 +0000 | [diff] [blame] | 124 | int optind; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 125 | |
| 126 | if (argc <= 1) |
| 127 | usage(); |
bellard | 586314f | 2003-03-03 15:02:29 +0000 | [diff] [blame] | 128 | loglevel = 0; |
| 129 | optind = 1; |
| 130 | if (argv[optind] && !strcmp(argv[optind], "-d")) { |
| 131 | loglevel = 1; |
| 132 | optind++; |
| 133 | } |
| 134 | filename = argv[optind]; |
| 135 | |
| 136 | /* init debug */ |
| 137 | if (loglevel) { |
| 138 | logfile = fopen(DEBUG_LOGFILE, "w"); |
| 139 | if (!logfile) { |
| 140 | perror(DEBUG_LOGFILE); |
| 141 | exit(1); |
| 142 | } |
| 143 | setvbuf(logfile, NULL, _IOLBF, 0); |
| 144 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 145 | |
| 146 | /* Zero out regs */ |
bellard | 01ffc75 | 2003-02-18 23:00:51 +0000 | [diff] [blame] | 147 | memset(regs, 0, sizeof(struct target_pt_regs)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 148 | |
| 149 | /* Zero out image_info */ |
| 150 | memset(info, 0, sizeof(struct image_info)); |
| 151 | |
bellard | 4b74fe1 | 2003-03-03 23:23:09 +0000 | [diff] [blame] | 152 | if(elf_exec(filename, argv+optind, environ, regs, info) != 0) { |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 153 | printf("Error loading %s\n", filename); |
| 154 | exit(1); |
| 155 | } |
| 156 | |
bellard | 4b74fe1 | 2003-03-03 23:23:09 +0000 | [diff] [blame] | 157 | if (loglevel) { |
| 158 | fprintf(logfile, "start_brk 0x%08lx\n" , info->start_brk); |
| 159 | fprintf(logfile, "end_code 0x%08lx\n" , info->end_code); |
| 160 | fprintf(logfile, "start_code 0x%08lx\n" , info->start_code); |
| 161 | fprintf(logfile, "end_data 0x%08lx\n" , info->end_data); |
| 162 | fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack); |
| 163 | fprintf(logfile, "brk 0x%08lx\n" , info->brk); |
| 164 | fprintf(logfile, "esp 0x%08lx\n" , regs->esp); |
| 165 | fprintf(logfile, "eip 0x%08lx\n" , regs->eip); |
| 166 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 167 | |
| 168 | target_set_brk((char *)info->brk); |
| 169 | syscall_init(); |
| 170 | |
bellard | 0ecfa99 | 2003-03-03 14:32:43 +0000 | [diff] [blame] | 171 | env = cpu_x86_init(); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 172 | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 173 | /* linux register setup */ |
bellard | 0ecfa99 | 2003-03-03 14:32:43 +0000 | [diff] [blame] | 174 | env->regs[R_EAX] = regs->eax; |
| 175 | env->regs[R_EBX] = regs->ebx; |
| 176 | env->regs[R_ECX] = regs->ecx; |
| 177 | env->regs[R_EDX] = regs->edx; |
| 178 | env->regs[R_ESI] = regs->esi; |
| 179 | env->regs[R_EDI] = regs->edi; |
| 180 | env->regs[R_EBP] = regs->ebp; |
| 181 | env->regs[R_ESP] = regs->esp; |
bellard | 0ecfa99 | 2003-03-03 14:32:43 +0000 | [diff] [blame] | 182 | env->pc = regs->eip; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 183 | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 184 | /* linux segment setup */ |
| 185 | env->gdt.base = (void *)gdt_table; |
| 186 | env->gdt.limit = sizeof(gdt_table) - 1; |
| 187 | write_dt(&gdt_table[__USER_CS >> 3], 0, 0xffffffff, 1); |
| 188 | write_dt(&gdt_table[__USER_DS >> 3], 0, 0xffffffff, 1); |
| 189 | cpu_x86_load_seg(env, R_CS, __USER_CS); |
| 190 | cpu_x86_load_seg(env, R_DS, __USER_DS); |
| 191 | cpu_x86_load_seg(env, R_ES, __USER_DS); |
| 192 | cpu_x86_load_seg(env, R_SS, __USER_DS); |
| 193 | cpu_x86_load_seg(env, R_FS, __USER_DS); |
| 194 | cpu_x86_load_seg(env, R_GS, __USER_DS); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 195 | |
| 196 | for(;;) { |
| 197 | int err; |
| 198 | uint8_t *pc; |
bellard | 0ecfa99 | 2003-03-03 14:32:43 +0000 | [diff] [blame] | 199 | |
| 200 | err = cpu_x86_exec(env); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 201 | switch(err) { |
| 202 | case EXCP0D_GPF: |
bellard | 0ecfa99 | 2003-03-03 14:32:43 +0000 | [diff] [blame] | 203 | pc = (uint8_t *)env->pc; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 204 | if (pc[0] == 0xcd && pc[1] == 0x80) { |
| 205 | /* syscall */ |
bellard | 0ecfa99 | 2003-03-03 14:32:43 +0000 | [diff] [blame] | 206 | env->pc += 2; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 207 | env->regs[R_EAX] = do_syscall(env, |
| 208 | env->regs[R_EAX], |
bellard | 0ecfa99 | 2003-03-03 14:32:43 +0000 | [diff] [blame] | 209 | env->regs[R_EBX], |
| 210 | env->regs[R_ECX], |
| 211 | env->regs[R_EDX], |
| 212 | env->regs[R_ESI], |
| 213 | env->regs[R_EDI], |
| 214 | env->regs[R_EBP]); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 215 | } else { |
| 216 | goto trap_error; |
| 217 | } |
| 218 | break; |
| 219 | default: |
| 220 | trap_error: |
bellard | 0ecfa99 | 2003-03-03 14:32:43 +0000 | [diff] [blame] | 221 | fprintf(stderr, "0x%08lx: Unknown exception %d, aborting\n", |
| 222 | (long)env->pc, err); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 223 | abort(); |
| 224 | } |
| 225 | } |
| 226 | return 0; |
| 227 | } |