/* * Example of use of user mode libqemu: launch a basic .com DOS * executable */ #include #include #include #include #include #include #include #include #include #include "cpu.h" //#define SIGTEST void cpu_outb(CPUState *env, int addr, int val) { fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val); } void cpu_outw(CPUState *env, int addr, int val) { fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val); } void cpu_outl(CPUState *env, int addr, int val) { fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val); } int cpu_inb(CPUState *env, int addr) { fprintf(stderr, "inb: port=0x%04x\n", addr); return 0; } int cpu_inw(CPUState *env, int addr) { fprintf(stderr, "inw: port=0x%04x\n", addr); return 0; } int cpu_inl(CPUState *env, int addr) { fprintf(stderr, "inl: port=0x%04x\n", addr); return 0; } int cpu_get_pic_interrupt(CPUState *env) { return -1; } uint64_t cpu_get_tsc(CPUState *env) { return 0; } static void set_gate(void *ptr, unsigned int type, unsigned int dpl, unsigned long addr, unsigned int sel) { unsigned int e1, e2; e1 = (addr & 0xffff) | (sel << 16); e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8); stl((uint8_t *)ptr, e1); stl((uint8_t *)ptr + 4, e2); } uint64_t idt_table[256]; /* only dpl matters as we do only user space emulation */ static void set_idt(int n, unsigned int dpl) { set_gate(idt_table + n, 0, dpl, 0, 0); } void qemu_free(void *ptr) { free(ptr); } void *qemu_malloc(size_t size) { return malloc(size); } void *qemu_mallocz(size_t size) { void *ptr; ptr = qemu_malloc(size); if (!ptr) return NULL; memset(ptr, 0, size); return ptr; } void *qemu_vmalloc(size_t size) { return memalign(4096, size); } void qemu_vfree(void *ptr) { free(ptr); } void qemu_printf(const char *fmt, ...) { va_list ap; va_start(ap, fmt); vprintf(fmt, ap); va_end(ap); } /* XXX: this is a bug in helper2.c */ int errno; /**********************************************/ #define COM_BASE_ADDR 0x10100 void usage(void) { printf("qruncom version 0.1 (c) 2003 Fabrice Bellard\n" "usage: qruncom file.com\n" "user mode libqemu demo: run simple .com DOS executables\n"); exit(1); } static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg) { return (uint8_t *)((seg << 4) + (reg & 0xffff)); } static inline void pushw(CPUState *env, int val) { env->regs[R_ESP] = (env->regs[R_ESP] & ~0xffff) | ((env->regs[R_ESP] - 2) & 0xffff); *(uint16_t *)seg_to_linear(env->segs[R_SS].selector, env->regs[R_ESP]) = val; } static void host_segv_handler(int host_signum, siginfo_t *info, void *puc) { if (cpu_signal_handler(host_signum, info, puc)) { return; } abort(); } int main(int argc, char **argv) { uint8_t *vm86_mem; const char *filename; int fd, ret, seg; CPUState *env; if (argc != 2) usage(); filename = argv[1]; vm86_mem = mmap((void *)0x00000000, 0x110000, PROT_WRITE | PROT_READ | PROT_EXEC, MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0); if (vm86_mem == MAP_FAILED) { perror("mmap"); exit(1); } /* load the MSDOS .com executable */ fd = open(filename, O_RDONLY); if (fd < 0) { perror(filename); exit(1); } ret = read(fd, vm86_mem + COM_BASE_ADDR, 65536 - 256); if (ret < 0) { perror("read"); exit(1); } close(fd); /* install exception handler for CPU emulator */ { struct sigaction act; sigfillset(&act.sa_mask); act.sa_flags = SA_SIGINFO; // act.sa_flags |= SA_ONSTACK; act.sa_sigaction = host_segv_handler; sigaction(SIGSEGV, &act, NULL); sigaction(SIGBUS, &act, NULL); #if defined (TARGET_I386) && defined(USE_CODE_COPY) sigaction(SIGFPE, &act, NULL); #endif } // cpu_set_log(CPU_LOG_TB_IN_ASM | CPU_LOG_TB_OUT_ASM | CPU_LOG_EXEC); env = cpu_init(); /* disable code copy to simplify debugging */ code_copy_enabled = 0; /* set user mode state (XXX: should be done automatically by cpu_init ?) */ env->user_mode_only = 1; cpu_x86_set_cpl(env, 3); env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK; /* NOTE: hflags duplicates some of the virtual CPU state */ env->hflags |= HF_PE_MASK | VM_MASK; /* flags setup : we activate the IRQs by default as in user mode. We also activate the VM86 flag to run DOS code */ env->eflags |= IF_MASK | VM_MASK; /* init basic registers */ env->eip = 0x100; env->regs[R_ESP] = 0xfffe; seg = (COM_BASE_ADDR - 0x100) >> 4; cpu_x86_load_seg_cache(env, R_CS, seg, (seg << 4), 0xffff, 0); cpu_x86_load_seg_cache(env, R_SS, seg,
# This file is dual licensed under the terms of the Apache License, Version
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.

from __future__ import absolute_import, division, print_function

INCLUDES = """
#include <openssl/engine.h>
"""

TYPES = """
typedef ... ENGINE;

static const long Cryptography_HAS_ENGINE;
"""

FUNCTIONS = """
ENGINE *ENGINE_by_id(const char *);
int ENGINE_init(ENGINE *);
int ENGINE_finish(ENGINE *);
ENGINE *ENGINE_get_default_RAND(void);
int ENGINE_set_default_RAND(ENGINE *);
void ENGINE_unregister_RAND(ENGINE *);
int ENGINE_ctrl_cmd(ENGINE *, const char *, long, void *, void (*)(void), int);
int ENGINE_free(ENGINE *);
const char *ENGINE_get_name(const ENGINE *);

"""

CUSTOMIZATIONS = """
#ifdef OPENSSL_NO_ENGINE
static const long Cryptography_HAS_ENGINE = 0;

ENGINE *(*ENGINE_by_id)(const char *) = NULL;
int (*ENGINE_init)(ENGINE *) = NULL;
int (*ENGINE_finish)(ENGINE *) = NULL;
ENGINE *(*ENGINE_get_default_RAND)(void) = NULL;
int (*ENGINE_set_default_RAND)(ENGINE *) = NULL;
void (*ENGINE_unregister_RAND)(ENGINE *) = NULL;
int (*ENGINE_ctrl_cmd)(ENGINE *, const char *, long, void *,
                       void (*)(void), int) = NULL;

int (*ENGINE_free)(ENGINE *) = NULL;
const char *(*ENGINE_get_id)(const ENGINE *) = NULL;
const char *(*ENGINE_get_name)(const ENGINE *) = NULL;

#else
static const long Cryptography_HAS_ENGINE = 1;
#endif
"""