diff options
| author | Prashanth Mundkur | 2018-10-02 12:02:00 -0700 |
|---|---|---|
| committer | Prashanth Mundkur | 2018-10-23 15:32:15 -0700 |
| commit | 2a511449bcd694a5a8e2d16fb65262c914861ba3 (patch) | |
| tree | ab1c1e0d45cad5937b339773cf723320766ca9b3 /riscv | |
| parent | 8525e0b26eaec05c2c031279693cd61c544fc12a (diff) | |
RISC-V: implement terminal output for C platform.
Diffstat (limited to 'riscv')
| -rw-r--r-- | riscv/riscv_platform.c | 7 | ||||
| -rw-r--r-- | riscv/riscv_platform_impl.c | 10 | ||||
| -rw-r--r-- | riscv/riscv_platform_impl.h | 3 | ||||
| -rw-r--r-- | riscv/riscv_sim.c | 39 |
4 files changed, 52 insertions, 7 deletions
diff --git a/riscv/riscv_platform.c b/riscv/riscv_platform.c index fa3b49aa..5eeb0eb7 100644 --- a/riscv/riscv_platform.c +++ b/riscv/riscv_platform.c @@ -48,8 +48,11 @@ unit cancel_reservation(unit u) return UNIT; } -unit plat_term_write(mach_bits c) -{ return UNIT; } +unit plat_term_write(mach_bits s) +{ char c = s & 0xff; + plat_term_write_impl(c); + return UNIT; +} void plat_insns_per_tick(sail_int *rop, unit u) { } diff --git a/riscv/riscv_platform_impl.c b/riscv/riscv_platform_impl.c index 3135895d..d8d52da0 100644 --- a/riscv/riscv_platform_impl.c +++ b/riscv/riscv_platform_impl.c @@ -1,4 +1,6 @@ #include "riscv_platform_impl.h" +#include <unistd.h> +#include <stdio.h> /* Settings of the platform implementation, with common defaults. */ @@ -16,3 +18,11 @@ uint64_t rv_clint_size = UINT64_C(0xc0000); uint64_t rv_htif_tohost = UINT64_C(0x80001000); uint64_t rv_insns_per_tick = UINT64_C(100); + +int term_fd = 1; // set during startup +void plat_term_write_impl(char c) +{ + if (write(term_fd, &c, sizeof(c)) < 0) { + fprintf(stderr, "Unable to write to terminal!\n"); + } +} diff --git a/riscv/riscv_platform_impl.h b/riscv/riscv_platform_impl.h index a3ab79f8..562f8554 100644 --- a/riscv/riscv_platform_impl.h +++ b/riscv/riscv_platform_impl.h @@ -22,3 +22,6 @@ extern uint64_t rv_clint_size; extern uint64_t rv_htif_tohost; extern uint64_t rv_insns_per_tick; + +extern int term_fd; +void plat_term_write_impl(char c); diff --git a/riscv/riscv_sim.c b/riscv/riscv_sim.c index 9e99136d..fc732e89 100644 --- a/riscv/riscv_sim.c +++ b/riscv/riscv_sim.c @@ -1,6 +1,11 @@ #include <getopt.h> #include <stdio.h> #include <stdlib.h> +#include <errno.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> #include "elf.h" #include "sail.h" @@ -34,12 +39,13 @@ struct tv_spike_t; static bool do_dump_dts = false; struct tv_spike_t *s = NULL; +char *term_log = NULL; static struct option options[] = { {"enable-dirty", no_argument, 0, 'd'}, {"enable-misaligned", no_argument, 0, 'm'}, {"dump-dts", no_argument, 0, 's'}, - {"verbosity", required_argument, 0, 'v'}, + {"terminal-log", required_argument, 0, 't'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; @@ -59,7 +65,7 @@ char *process_args(int argc, char **argv) { int c, idx = 1; while(true) { - c = getopt_long(argc, argv, "dmsv:h", options, &idx); + c = getopt_long(argc, argv, "dmst:v:h", options, &idx); if (c == -1) break; switch (c) { case 'd': @@ -71,6 +77,9 @@ char *process_args(int argc, char **argv) case 's': do_dump_dts = true; break; + case 't': + term_log = strdup(optarg); + break; case 'h': print_usage(argv[0], 0); break; @@ -79,8 +88,8 @@ char *process_args(int argc, char **argv) print_usage(argv[0], 1); } } - if (idx >= argc) print_usage(argv[0], 0); + if (term_log == NULL) term_log = strdup("term.log"); return argv[idx]; } @@ -146,7 +155,7 @@ void init_sail_reset_vector(uint64_t entry) for (int i = 0; i < dtb_len; i++) write_mem(addr++, dtb[i]); #else - fprintf(stdout, "Running without rom device tree.\n"); + fprintf(stderr, "Running without rom device tree.\n"); /* TODO: write DTB */ #endif @@ -327,13 +336,32 @@ void run_sail(void) finish(diverged); step_exception: - fprintf(stdout, "Sail exception!"); + fprintf(stderr, "Sail exception!"); goto dump_state; } +void init_logs() +{ +#ifdef SPIKE + // The Spike interface uses stdout for terminal output, and stderr for logs. + // Do the same here. + int logfd; + if (dup2(1, 2) < 0) { + fprintf(stderr, "Unable to dup 1 -> 2: %s\n", strerror(errno)); + exit(1); + } + if ((term_fd = open(term_log, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR)) < 0) { + fprintf(stderr, "Cannot create terminal log '%s': %s\n", term_log, strerror(errno)); + exit(1); + } +#endif +} + int main(int argc, char **argv) { char *file = process_args(argc, argv); + init_logs(); + uint64_t entry = load_sail(file); /* initialize spike before sail so that we can access the device-tree blob, @@ -345,4 +373,5 @@ int main(int argc, char **argv) if (!init_check(s)) finish(1); run_sail(); + flush_logs(); } |
