int write_next_target(struct target *target, uint64_t addr, uint64_t value) { assert(target->next); return write_target(target->next, target->base + addr, value); }
int rx_syscall (int id) { static char buf[256]; int rv; argp = 0; stackp = 4; if (trace) printf ("\033[31m/* SYSCALL(%d) = %s */\033[0m\n", id, id <= SYS_link ? callnames[id] : "unknown"); switch (id) { case SYS_exit: { int ec = arg (); if (verbose) printf ("[exit %d]\n", ec); return RX_MAKE_EXITED (ec); } break; case SYS_open: { int path = arg (); /* The open function is defined as taking a variable number of arguments because the third parameter to it is optional: open (const char * filename, int flags, ...); Hence the oflags and cflags arguments will be on the stack and we need to skip the (empty) argument registers r3 and r4. */ argp = 4; int oflags = arg (); int cflags = arg (); read_target (buf, path, 256, 1); if (trace) printf ("open(\"%s\",0x%x,%#o) = ", buf, oflags, cflags); if (callbacks) /* The callback vector ignores CFLAGS. */ rv = callbacks->open (callbacks, buf, oflags); else { int h_oflags = 0; if (oflags & 0x0001) h_oflags |= O_WRONLY; if (oflags & 0x0002) h_oflags |= O_RDWR; if (oflags & 0x0200) h_oflags |= O_CREAT; if (oflags & 0x0008) h_oflags |= O_APPEND; if (oflags & 0x0400) h_oflags |= O_TRUNC; rv = open (buf, h_oflags, cflags); } if (trace) printf ("%d\n", rv); put_reg (1, rv); } break; case SYS_close: { int fd = arg (); if (callbacks) rv = callbacks->close (callbacks, fd); else if (fd > 2) rv = close (fd); else rv = 0; if (trace) printf ("close(%d) = %d\n", fd, rv); put_reg (1, rv); } break; case SYS_read: { int fd = arg (); int addr = arg (); int count = arg (); if (count > sizeof (buf)) count = sizeof (buf); if (callbacks) rv = callbacks->read (callbacks, fd, buf, count); else rv = read (fd, buf, count); if (trace) printf ("read(%d,%d) = %d\n", fd, count, rv); if (rv > 0) write_target (buf, addr, rv, 0); put_reg (1, rv); } break; case SYS_write: { int fd = arg (); int addr = arg (); int count = arg (); if (count > sizeof (buf)) count = sizeof (buf); if (trace) printf ("write(%d,0x%x,%d)\n", fd, addr, count); read_target (buf, addr, count, 0); if (trace) fflush (stdout); if (callbacks) rv = callbacks->write (callbacks, fd, buf, count); else rv = write (fd, buf, count); if (trace) printf ("write(%d,%d) = %d\n", fd, count, rv); put_reg (1, rv); } break; case SYS_getpid: put_reg (1, 42); break; case SYS_gettimeofday: { int tvaddr = arg (); struct timeval tv; rv = gettimeofday (&tv, 0); if (trace) printf ("gettimeofday: %ld sec %ld usec to 0x%x\n", tv.tv_sec, tv.tv_usec, tvaddr); mem_put_si (tvaddr, tv.tv_sec); mem_put_si (tvaddr + 4, tv.tv_usec); put_reg (1, rv); } break; case SYS_kill: { int pid = arg (); int sig = arg (); if (pid == 42) { if (verbose) printf ("[signal %d]\n", sig); return RX_MAKE_STOPPED (sig); } } break; case 11: { int heaptop_arg = arg (); if (trace) printf ("sbrk: heap top set to %x\n", heaptop_arg); heaptop = heaptop_arg; if (heapbottom == 0) heapbottom = heaptop_arg; } break; case 255: { int addr = arg (); mem_put_si (addr, rx_cycles + mem_usage_cycles()); } break; } return RX_MAKE_STEPPED (); }
void m32c_syscall (int id) { static char buf[256]; int rv; argp = 0; stackp = A16 ? 3 : 4; if (trace) printf ("\033[31m/* SYSCALL(%d) = %s */\033[0m\n", id, callnames[id]); switch (id) { case SYS_exit: { int ec = arg (2); if (verbose) printf ("[exit %d]\n", ec); step_result = M32C_MAKE_EXITED (ec); } break; case SYS_open: { int path = arg (PTRSZ); int oflags = arg (2); int cflags = arg (2); read_target (buf, path, 256, 1); if (trace) printf ("open(\"%s\",0x%x,%#o) = ", buf, oflags, cflags); if (callbacks) /* The callback vector ignores CFLAGS. */ rv = callbacks->open (callbacks, buf, oflags); else { int h_oflags = 0; if (oflags & 0x0001) h_oflags |= O_WRONLY; if (oflags & 0x0002) h_oflags |= O_RDWR; if (oflags & 0x0200) h_oflags |= O_CREAT; if (oflags & 0x0008) h_oflags |= O_APPEND; if (oflags & 0x0400) h_oflags |= O_TRUNC; rv = open (buf, h_oflags, cflags); } if (trace) printf ("%d\n", rv); put_reg (r0, rv); } break; case SYS_close: { int fd = arg (2); if (callbacks) rv = callbacks->close (callbacks, fd); else if (fd > 2) rv = close (fd); else rv = 0; if (trace) printf ("close(%d) = %d\n", fd, rv); put_reg (r0, rv); } break; case SYS_read: { int fd = arg (2); int addr = arg (PTRSZ); int count = arg (2); if (count > sizeof (buf)) count = sizeof (buf); if (callbacks) rv = callbacks->read (callbacks, fd, buf, count); else rv = read (fd, buf, count); if (trace) printf ("read(%d,%d) = %d\n", fd, count, rv); if (rv > 0) write_target (buf, addr, rv, 0); put_reg (r0, rv); } break; case SYS_write: { int fd = arg (2); int addr = arg (PTRSZ); int count = arg (2); if (count > sizeof (buf)) count = sizeof (buf); if (trace) printf ("write(%d,0x%x,%d)\n", fd, addr, count); read_target (buf, addr, count, 0); if (trace) fflush (stdout); if (callbacks) rv = callbacks->write (callbacks, fd, buf, count); else rv = write (fd, buf, count); if (trace) printf ("write(%d,%d) = %d\n", fd, count, rv); put_reg (r0, rv); } break; case SYS_getpid: put_reg (r0, 42); break; case SYS_gettimeofday: { int tvaddr = arg (PTRSZ); struct timeval tv; rv = gettimeofday (&tv, 0); if (trace) printf ("gettimeofday: %ld sec %ld usec to 0x%x\n", tv.tv_sec, tv.tv_usec, tvaddr); mem_put_si (tvaddr, tv.tv_sec); mem_put_si (tvaddr + 4, tv.tv_usec); put_reg (r0, rv); } break; case SYS_kill: { int pid = arg (2); int sig = arg (2); if (pid == 42) { if (verbose) printf ("[signal %d]\n", sig); step_result = M32C_MAKE_STOPPED (sig); } } break; case 11: { int heaptop_arg = arg (PTRSZ); if (trace) printf ("sbrk: heap top set to %x\n", heaptop_arg); heaptop = heaptop_arg; if (heapbottom == 0) heapbottom = heaptop_arg; } break; } }
/* Return size bytes of memory in *output. *output must point to an * array large enough to hold size bytes. */ int adu_getmem(struct target *target, uint64_t start_addr, uint8_t *output, uint64_t size) { int rc = 0; uint64_t addr, cmd_reg, ctrl_reg, val; CHECK_ERR(adu_lock(target)); ctrl_reg = TTYPE_TREAD; ctrl_reg = SETFIELD(FBC_ALTD_TTYPE, ctrl_reg, TTYPE_DMA_PARTIAL_READ); ctrl_reg = SETFIELD(FBC_ALTD_TSIZE, ctrl_reg, 8); CHECK_ERR(read_target(target, ALTD_CMD_REG, &cmd_reg)); cmd_reg |= FBC_ALTD_START_OP; cmd_reg = SETFIELD(FBC_ALTD_SCOPE, cmd_reg, SCOPE_SYSTEM); cmd_reg = SETFIELD(FBC_ALTD_DROP_PRIORITY, cmd_reg, DROP_PRIORITY_MEDIUM); /* We read data in 8-byte aligned chunks */ for (addr = 8*(start_addr / 8); addr < start_addr + size; addr += 8) { uint64_t data; retry: /* Clear status bits */ CHECK_ERR(adu_reset(target)); /* Set the address */ ctrl_reg = SETFIELD(FBC_ALTD_ADDRESS, ctrl_reg, addr); CHECK_ERR(write_target(target, ALTD_CONTROL_REG, ctrl_reg)); /* Start the command */ CHECK_ERR(write_target(target, ALTD_CMD_REG, cmd_reg)); /* Wait for completion */ do { CHECK_ERR(read_target(target, ALTD_STATUS_REG, &val)); } while (!val); if( !(val & FBC_ALTD_ADDR_DONE) || !(val & FBC_ALTD_DATA_DONE)) { /* PBINIT_MISSING is expected occasionally so just retry */ if (val & FBC_ALTD_PBINIT_MISSING) goto retry; else { PR_ERROR("Unable to read memory. " \ "ALTD_STATUS_REG = 0x%016llx\n", val); rc = -1; break; } } /* Read data */ CHECK_ERR(read_target(target, ALTD_DATA_REG, &data)); /* ADU returns data in big-endian form in the register */ data = __builtin_bswap64(data); if (addr < start_addr) { memcpy(output, ((uint8_t *) &data) + (start_addr - addr), 8 - (start_addr - addr)); output += 8 - (start_addr - addr); } else if (addr + 8 > start_addr + size) { memcpy(output, &data, start_addr + size - addr); } else { memcpy(output, &data, 8); output += 8; } } adu_unlock(target); return rc; }