From 5b2c75563df56975faa9dbf3e6999f9189a0d55e Mon Sep 17 00:00:00 2001 From: Daniel Beer Date: Thu, 5 Aug 2010 14:43:07 +1200 Subject: [PATCH] Device interface and drivers are now 32-bit clean. --- bsl.c | 15 ++++++++++----- cproc_util.c | 2 +- cproc_util.h | 2 +- devcmd.c | 6 +++--- device.c | 2 +- device.h | 19 ++++++++++--------- fet.c | 21 ++++++++++++--------- gdb.c | 8 ++++---- sim.c | 33 +++++++++++++++++++++++---------- 9 files changed, 65 insertions(+), 43 deletions(-) diff --git a/bsl.c b/bsl.c index 48aa182..6673425 100644 --- a/bsl.c +++ b/bsl.c @@ -222,32 +222,37 @@ static device_status_t bsl_poll(device_t dev_base) return DEVICE_STATUS_HALTED; } -static int bsl_getregs(device_t dev_base, uint16_t *regs) +static int bsl_getregs(device_t dev_base, address_t *regs) { fprintf(stderr, "bsl: register fetch is not implemented\n"); return -1; } -static int bsl_setregs(device_t dev_base, const uint16_t *regs) +static int bsl_setregs(device_t dev_base, const address_t *regs) { fprintf(stderr, "bsl: register store is not implemented\n"); return -1; } static int bsl_writemem(device_t dev_base, - uint16_t addr, const uint8_t *mem, int len) + address_t addr, const uint8_t *mem, address_t len) { fprintf(stderr, "bsl: memory write is not implemented\n"); return -1; } static int bsl_readmem(device_t dev_base, - uint16_t addr, uint8_t *mem, int len) + address_t addr, uint8_t *mem, address_t len) { struct bsl_device *dev = (struct bsl_device *)dev_base; + if ((addr | len | (addr + len)) & 0xffff0000) { + fprintf(stderr, "bsl: memory read out of range\n"); + return -1; + } + while (len) { - int count = len; + address_t count = len; if (count > 128) count = 128; diff --git a/cproc_util.c b/cproc_util.c index 95efcbe..8f6cdd0 100644 --- a/cproc_util.c +++ b/cproc_util.c @@ -280,7 +280,7 @@ void cproc_hexdump(cproc_t cp, uint16_t addr, const uint8_t *data, int data_len) } } -void cproc_regs(cproc_t cp, const uint16_t *regs) +void cproc_regs(cproc_t cp, const address_t *regs) { int i; diff --git a/cproc_util.h b/cproc_util.h index 4e16602..b0de89c 100644 --- a/cproc_util.h +++ b/cproc_util.h @@ -31,6 +31,6 @@ void cproc_hexdump(cproc_t cp, uint16_t addr, const uint8_t *buf, int len); /* Colorized register dump */ -void cproc_regs(cproc_t cp, const uint16_t *regs); +void cproc_regs(cproc_t cp, const address_t *regs); #endif diff --git a/devcmd.c b/devcmd.c index 791ddc9..2a9fefd 100644 --- a/devcmd.c +++ b/devcmd.c @@ -34,7 +34,7 @@ static int cmd_regs(cproc_t cp, char **arg) { device_t dev = cproc_device(cp); - uint16_t regs[DEVICE_NUM_REGS]; + address_t regs[DEVICE_NUM_REGS]; uint8_t code[16]; int len = sizeof(code); @@ -184,7 +184,7 @@ static int cmd_run(cproc_t cp, char **arg) { device_t dev = cproc_device(cp); device_status_t status; - uint16_t regs[DEVICE_NUM_REGS]; + address_t regs[DEVICE_NUM_REGS]; if (dev->getregs(dev, regs) < 0) { fprintf(stderr, "warning: device: can't fetch registers\n"); @@ -237,7 +237,7 @@ static int cmd_set(cproc_t cp, char **arg) char *val_text = get_arg(arg); int reg; address_t value = 0; - uint16_t regs[DEVICE_NUM_REGS]; + address_t regs[DEVICE_NUM_REGS]; if (!(reg_text && val_text)) { fprintf(stderr, "set: must specify a register and a value\n"); diff --git a/device.c b/device.c index e0777f2..ad65f68 100644 --- a/device.c +++ b/device.c @@ -60,7 +60,7 @@ static void delbrk(device_t dev, uint16_t addr) } } -int device_setbrk(device_t dev, int which, int enabled, uint16_t addr) +int device_setbrk(device_t dev, int which, int enabled, address_t addr) { if (which < 0) { if (enabled) diff --git a/device.h b/device.h index 54e6670..216b274 100644 --- a/device.h +++ b/device.h @@ -20,6 +20,7 @@ #define DEVICE_H_ #include +#include "util.h" struct device; typedef struct device *device_t; @@ -46,8 +47,8 @@ typedef enum { #define DEVICE_BP_DIRTY 0x02 struct device_breakpoint { - uint16_t addr; - int flags; + address_t addr; + int flags; }; struct device { @@ -63,14 +64,14 @@ struct device { void (*destroy)(device_t dev); /* Read/write memory */ - int (*readmem)(device_t dev, uint16_t addr, - uint8_t *mem, int len); - int (*writemem)(device_t dev, uint16_t addr, - const uint8_t *mem, int len); + int (*readmem)(device_t dev, address_t addr, + uint8_t *mem, address_t len); + int (*writemem)(device_t dev, address_t addr, + const uint8_t *mem, address_t len); /* Read/write registers */ - int (*getregs)(device_t dev, uint16_t *regs); - int (*setregs)(device_t dev, const uint16_t *regs); + int (*getregs)(device_t dev, address_t *regs); + int (*setregs)(device_t dev, const address_t *regs); /* CPU control */ int (*ctl)(device_t dev, device_ctl_t op); @@ -87,6 +88,6 @@ struct device { * modified. Otherwise, if which < 0, breakpoint slots are selected * automatically. */ -int device_setbrk(device_t dev, int which, int enabled, uint16_t address); +int device_setbrk(device_t dev, int which, int enabled, address_t address); #endif diff --git a/fet.c b/fet.c index 06e1bd6..cfb41fb 100644 --- a/fet.c +++ b/fet.c @@ -42,7 +42,7 @@ struct fet_device { int version; /* Device-specific information */ - u_int16_t code_start; + uint16_t code_start; uint8_t fet_buf[65538]; int fet_len; @@ -443,7 +443,7 @@ static int xfer(struct fet_device *dev, va_start(ap, nparams); for (i = 0; i < nparams; i++) - params[i] = va_arg(ap, unsigned int); + params[i] = va_arg(ap, uint32_t); va_end(ap); if (data && (dev->proto_flags & FET_PROTO_RF2500)) { @@ -707,7 +707,8 @@ static void fet_destroy(device_t dev_base) free(dev); } -int fet_readmem(device_t dev_base, uint16_t addr, uint8_t *buffer, int count) +int fet_readmem(device_t dev_base, address_t addr, uint8_t *buffer, + address_t count) { struct fet_device *dev = (struct fet_device *)dev_base; @@ -735,8 +736,8 @@ int fet_readmem(device_t dev_base, uint16_t addr, uint8_t *buffer, int count) return 0; } -int fet_writemem(device_t dev_base, uint16_t addr, - const uint8_t *buffer, int count) +int fet_writemem(device_t dev_base, address_t addr, + const uint8_t *buffer, address_t count) { struct fet_device *dev = (struct fet_device *)dev_base; @@ -760,7 +761,7 @@ int fet_writemem(device_t dev_base, uint16_t addr, return 0; } -static int fet_getregs(device_t dev_base, uint16_t *regs) +static int fet_getregs(device_t dev_base, address_t *regs) { struct fet_device *dev = (struct fet_device *)dev_base; int i; @@ -775,12 +776,12 @@ static int fet_getregs(device_t dev_base, uint16_t *regs) } for (i = 0; i < DEVICE_NUM_REGS; i++) - regs[i] = LE_WORD(dev->fet_reply.data, i * 4); + regs[i] = LE_LONG(dev->fet_reply.data, i * 4); return 0; } -static int fet_setregs(device_t dev_base, const uint16_t *regs) +static int fet_setregs(device_t dev_base, const address_t *regs) { struct fet_device *dev = (struct fet_device *)dev_base; uint8_t buf[DEVICE_NUM_REGS * 4];; @@ -791,7 +792,9 @@ static int fet_setregs(device_t dev_base, const uint16_t *regs) for (i = 0; i < DEVICE_NUM_REGS; i++) { buf[i * 4] = regs[i] & 0xff; - buf[i * 4 + 1] = regs[i] >> 8; + buf[i * 4 + 1] = (regs[i] >> 8) & 0xff; + buf[i * 4 + 2] = (regs[i] >> 16) & 0xff; + buf[i * 4 + 3] = regs[i] >> 24; } ret = xfer(dev, C_WRITEREGISTERS, buf, sizeof(buf), 1, 0xffff); diff --git a/gdb.c b/gdb.c index 72b89a9..0eabab7 100644 --- a/gdb.c +++ b/gdb.c @@ -213,7 +213,7 @@ static int gdb_send(struct gdb_data *data, const char *msg) static int read_registers(struct gdb_data *data) { - uint16_t regs[DEVICE_NUM_REGS]; + address_t regs[DEVICE_NUM_REGS]; int i; printf("Reading registers\n"); @@ -258,7 +258,7 @@ static int monitor_command(struct gdb_data *data, char *buf) static int write_registers(struct gdb_data *data, char *buf) { - uint16_t regs[DEVICE_NUM_REGS]; + address_t regs[DEVICE_NUM_REGS]; int i; if (strlen(buf) < DEVICE_NUM_REGS * 4) @@ -352,7 +352,7 @@ static int write_memory(struct gdb_data *data, char *text) static int run_set_pc(struct gdb_data *data, char *buf) { - uint16_t regs[DEVICE_NUM_REGS]; + address_t regs[DEVICE_NUM_REGS]; if (!*buf) return 0; @@ -366,7 +366,7 @@ static int run_set_pc(struct gdb_data *data, char *buf) static int run_final_status(struct gdb_data *data) { - uint16_t regs[DEVICE_NUM_REGS]; + address_t regs[DEVICE_NUM_REGS]; int i; if (data->device->getregs(data->device, regs) < 0) diff --git a/sim.c b/sim.c index 55d4a73..4e71ad4 100644 --- a/sim.c +++ b/sim.c @@ -440,11 +440,17 @@ static void sim_destroy(device_t dev_base) free(dev_base); } -static int sim_readmem(device_t dev_base, uint16_t addr, - uint8_t *mem, int len) +static int sim_readmem(device_t dev_base, address_t addr, + uint8_t *mem, address_t len) { struct sim_device *dev = (struct sim_device *)dev_base; + if (addr > MEM_SIZE || (addr + len) < addr || + (addr + len) > MEM_SIZE) { + fprintf(stderr, "sim: memory read out of range\n"); + return -1; + } + if (addr + len > MEM_SIZE) len = MEM_SIZE - addr; @@ -452,31 +458,38 @@ static int sim_readmem(device_t dev_base, uint16_t addr, return 0; } -static int sim_writemem(device_t dev_base, uint16_t addr, - const uint8_t *mem, int len) +static int sim_writemem(device_t dev_base, address_t addr, + const uint8_t *mem, address_t len) { struct sim_device *dev = (struct sim_device *)dev_base; - if (addr + len > MEM_SIZE) - len = MEM_SIZE - addr; + if (addr > MEM_SIZE || (addr + len) < addr || + (addr + len) > MEM_SIZE) { + fprintf(stderr, "sim: memory write out of range\n"); + return -1; + } memcpy(dev->memory + addr, mem, len); return 0; } -static int sim_getregs(device_t dev_base, uint16_t *regs) +static int sim_getregs(device_t dev_base, address_t *regs) { struct sim_device *dev = (struct sim_device *)dev_base; + int i; - memcpy(regs, dev->regs, sizeof(dev->regs)); + for (i = 0; i < DEVICE_NUM_REGS; i++) + regs[i] = dev->regs[i]; return 0; } -static int sim_setregs(device_t dev_base, const uint16_t *regs) +static int sim_setregs(device_t dev_base, const address_t *regs) { struct sim_device *dev = (struct sim_device *)dev_base; + int i; - memcpy(dev->regs, regs, sizeof(dev->regs)); + for (i = 0; i < DEVICE_NUM_REGS; i++) + dev->regs[i] = regs[i]; return 0; }