NJU-ProjectN/nemu ics2023 initialized
NJU-ProjectN/nemu eb63cf3568dbf4e0c3c6ef462e6ec685550fabbc Merge pull request #76 from rijuyuezhu/master
This commit is contained in:
parent
1efe03efb9
commit
2824efad33
141 changed files with 19573 additions and 0 deletions
162
nemu/src/monitor/monitor.c
Normal file
162
nemu/src/monitor/monitor.c
Normal file
|
@ -0,0 +1,162 @@
|
|||
/***************************************************************************************
|
||||
* Copyright (c) 2014-2022 Zihao Yu, Nanjing University
|
||||
*
|
||||
* NEMU is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* See the Mulan PSL v2 for more details.
|
||||
***************************************************************************************/
|
||||
|
||||
#include <isa.h>
|
||||
#include <memory/paddr.h>
|
||||
|
||||
void init_rand();
|
||||
void init_log(const char *log_file);
|
||||
void init_mem();
|
||||
void init_difftest(char *ref_so_file, long img_size, int port);
|
||||
void init_device();
|
||||
void init_sdb();
|
||||
void init_disasm(const char *triple);
|
||||
|
||||
static void welcome() {
|
||||
Log("Trace: %s", MUXDEF(CONFIG_TRACE, ANSI_FMT("ON", ANSI_FG_GREEN), ANSI_FMT("OFF", ANSI_FG_RED)));
|
||||
IFDEF(CONFIG_TRACE, Log("If trace is enabled, a log file will be generated "
|
||||
"to record the trace. This may lead to a large log file. "
|
||||
"If it is not necessary, you can disable it in menuconfig"));
|
||||
Log("Build time: %s, %s", __TIME__, __DATE__);
|
||||
printf("Welcome to %s-NEMU!\n", ANSI_FMT(str(__GUEST_ISA__), ANSI_FG_YELLOW ANSI_BG_RED));
|
||||
printf("For help, type \"help\"\n");
|
||||
Log("Exercise: Please remove me in the source code and compile NEMU again.");
|
||||
assert(0);
|
||||
}
|
||||
|
||||
#ifndef CONFIG_TARGET_AM
|
||||
#include <getopt.h>
|
||||
|
||||
void sdb_set_batch_mode();
|
||||
|
||||
static char *log_file = NULL;
|
||||
static char *diff_so_file = NULL;
|
||||
static char *img_file = NULL;
|
||||
static int difftest_port = 1234;
|
||||
|
||||
static long load_img() {
|
||||
if (img_file == NULL) {
|
||||
Log("No image is given. Use the default build-in image.");
|
||||
return 4096; // built-in image size
|
||||
}
|
||||
|
||||
FILE *fp = fopen(img_file, "rb");
|
||||
Assert(fp, "Can not open '%s'", img_file);
|
||||
|
||||
fseek(fp, 0, SEEK_END);
|
||||
long size = ftell(fp);
|
||||
|
||||
Log("The image is %s, size = %ld", img_file, size);
|
||||
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
int ret = fread(guest_to_host(RESET_VECTOR), size, 1, fp);
|
||||
assert(ret == 1);
|
||||
|
||||
fclose(fp);
|
||||
return size;
|
||||
}
|
||||
|
||||
static int parse_args(int argc, char *argv[]) {
|
||||
const struct option table[] = {
|
||||
{"batch" , no_argument , NULL, 'b'},
|
||||
{"log" , required_argument, NULL, 'l'},
|
||||
{"diff" , required_argument, NULL, 'd'},
|
||||
{"port" , required_argument, NULL, 'p'},
|
||||
{"help" , no_argument , NULL, 'h'},
|
||||
{0 , 0 , NULL, 0 },
|
||||
};
|
||||
int o;
|
||||
while ( (o = getopt_long(argc, argv, "-bhl:d:p:", table, NULL)) != -1) {
|
||||
switch (o) {
|
||||
case 'b': sdb_set_batch_mode(); break;
|
||||
case 'p': sscanf(optarg, "%d", &difftest_port); break;
|
||||
case 'l': log_file = optarg; break;
|
||||
case 'd': diff_so_file = optarg; break;
|
||||
case 1: img_file = optarg; return 0;
|
||||
default:
|
||||
printf("Usage: %s [OPTION...] IMAGE [args]\n\n", argv[0]);
|
||||
printf("\t-b,--batch run with batch mode\n");
|
||||
printf("\t-l,--log=FILE output log to FILE\n");
|
||||
printf("\t-d,--diff=REF_SO run DiffTest with reference REF_SO\n");
|
||||
printf("\t-p,--port=PORT run DiffTest with port PORT\n");
|
||||
printf("\n");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void init_monitor(int argc, char *argv[]) {
|
||||
/* Perform some global initialization. */
|
||||
|
||||
/* Parse arguments. */
|
||||
parse_args(argc, argv);
|
||||
|
||||
/* Set random seed. */
|
||||
init_rand();
|
||||
|
||||
/* Open the log file. */
|
||||
init_log(log_file);
|
||||
|
||||
/* Initialize memory. */
|
||||
init_mem();
|
||||
|
||||
/* Initialize devices. */
|
||||
IFDEF(CONFIG_DEVICE, init_device());
|
||||
|
||||
/* Perform ISA dependent initialization. */
|
||||
init_isa();
|
||||
|
||||
/* Load the image to memory. This will overwrite the built-in image. */
|
||||
long img_size = load_img();
|
||||
|
||||
/* Initialize differential testing. */
|
||||
init_difftest(diff_so_file, img_size, difftest_port);
|
||||
|
||||
/* Initialize the simple debugger. */
|
||||
init_sdb();
|
||||
|
||||
#ifndef CONFIG_ISA_loongarch32r
|
||||
IFDEF(CONFIG_ITRACE, init_disasm(
|
||||
MUXDEF(CONFIG_ISA_x86, "i686",
|
||||
MUXDEF(CONFIG_ISA_mips32, "mipsel",
|
||||
MUXDEF(CONFIG_ISA_riscv,
|
||||
MUXDEF(CONFIG_RV64, "riscv64",
|
||||
"riscv32"),
|
||||
"bad"))) "-pc-linux-gnu"
|
||||
));
|
||||
#endif
|
||||
|
||||
/* Display welcome message. */
|
||||
welcome();
|
||||
}
|
||||
#else // CONFIG_TARGET_AM
|
||||
static long load_img() {
|
||||
extern char bin_start, bin_end;
|
||||
size_t size = &bin_end - &bin_start;
|
||||
Log("img size = %ld", size);
|
||||
memcpy(guest_to_host(RESET_VECTOR), &bin_start, size);
|
||||
return size;
|
||||
}
|
||||
|
||||
void am_init_monitor() {
|
||||
init_rand();
|
||||
init_mem();
|
||||
init_isa();
|
||||
load_img();
|
||||
IFDEF(CONFIG_DEVICE, init_device());
|
||||
welcome();
|
||||
}
|
||||
#endif
|
125
nemu/src/monitor/sdb/expr.c
Normal file
125
nemu/src/monitor/sdb/expr.c
Normal file
|
@ -0,0 +1,125 @@
|
|||
/***************************************************************************************
|
||||
* Copyright (c) 2014-2022 Zihao Yu, Nanjing University
|
||||
*
|
||||
* NEMU is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* See the Mulan PSL v2 for more details.
|
||||
***************************************************************************************/
|
||||
|
||||
#include <isa.h>
|
||||
|
||||
/* We use the POSIX regex functions to process regular expressions.
|
||||
* Type 'man regex' for more information about POSIX regex functions.
|
||||
*/
|
||||
#include <regex.h>
|
||||
|
||||
enum {
|
||||
TK_NOTYPE = 256, TK_EQ,
|
||||
|
||||
/* TODO: Add more token types */
|
||||
|
||||
};
|
||||
|
||||
static struct rule {
|
||||
const char *regex;
|
||||
int token_type;
|
||||
} rules[] = {
|
||||
|
||||
/* TODO: Add more rules.
|
||||
* Pay attention to the precedence level of different rules.
|
||||
*/
|
||||
|
||||
{" +", TK_NOTYPE}, // spaces
|
||||
{"\\+", '+'}, // plus
|
||||
{"==", TK_EQ}, // equal
|
||||
};
|
||||
|
||||
#define NR_REGEX ARRLEN(rules)
|
||||
|
||||
static regex_t re[NR_REGEX] = {};
|
||||
|
||||
/* Rules are used for many times.
|
||||
* Therefore we compile them only once before any usage.
|
||||
*/
|
||||
void init_regex() {
|
||||
int i;
|
||||
char error_msg[128];
|
||||
int ret;
|
||||
|
||||
for (i = 0; i < NR_REGEX; i ++) {
|
||||
ret = regcomp(&re[i], rules[i].regex, REG_EXTENDED);
|
||||
if (ret != 0) {
|
||||
regerror(ret, &re[i], error_msg, 128);
|
||||
panic("regex compilation failed: %s\n%s", error_msg, rules[i].regex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
typedef struct token {
|
||||
int type;
|
||||
char str[32];
|
||||
} Token;
|
||||
|
||||
static Token tokens[32] __attribute__((used)) = {};
|
||||
static int nr_token __attribute__((used)) = 0;
|
||||
|
||||
static bool make_token(char *e) {
|
||||
int position = 0;
|
||||
int i;
|
||||
regmatch_t pmatch;
|
||||
|
||||
nr_token = 0;
|
||||
|
||||
while (e[position] != '\0') {
|
||||
/* Try all rules one by one. */
|
||||
for (i = 0; i < NR_REGEX; i ++) {
|
||||
if (regexec(&re[i], e + position, 1, &pmatch, 0) == 0 && pmatch.rm_so == 0) {
|
||||
char *substr_start = e + position;
|
||||
int substr_len = pmatch.rm_eo;
|
||||
|
||||
Log("match rules[%d] = \"%s\" at position %d with len %d: %.*s",
|
||||
i, rules[i].regex, position, substr_len, substr_len, substr_start);
|
||||
|
||||
position += substr_len;
|
||||
|
||||
/* TODO: Now a new token is recognized with rules[i]. Add codes
|
||||
* to record the token in the array `tokens'. For certain types
|
||||
* of tokens, some extra actions should be performed.
|
||||
*/
|
||||
|
||||
switch (rules[i].token_type) {
|
||||
default: TODO();
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (i == NR_REGEX) {
|
||||
printf("no match at position %d\n%s\n%*.s^\n", position, e, position, "");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
word_t expr(char *e, bool *success) {
|
||||
if (!make_token(e)) {
|
||||
*success = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* TODO: Insert codes to evaluate the expression. */
|
||||
TODO();
|
||||
|
||||
return 0;
|
||||
}
|
143
nemu/src/monitor/sdb/sdb.c
Normal file
143
nemu/src/monitor/sdb/sdb.c
Normal file
|
@ -0,0 +1,143 @@
|
|||
/***************************************************************************************
|
||||
* Copyright (c) 2014-2022 Zihao Yu, Nanjing University
|
||||
*
|
||||
* NEMU is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* See the Mulan PSL v2 for more details.
|
||||
***************************************************************************************/
|
||||
|
||||
#include <isa.h>
|
||||
#include <cpu/cpu.h>
|
||||
#include <readline/readline.h>
|
||||
#include <readline/history.h>
|
||||
#include "sdb.h"
|
||||
|
||||
static int is_batch_mode = false;
|
||||
|
||||
void init_regex();
|
||||
void init_wp_pool();
|
||||
|
||||
/* We use the `readline' library to provide more flexibility to read from stdin. */
|
||||
static char* rl_gets() {
|
||||
static char *line_read = NULL;
|
||||
|
||||
if (line_read) {
|
||||
free(line_read);
|
||||
line_read = NULL;
|
||||
}
|
||||
|
||||
line_read = readline("(nemu) ");
|
||||
|
||||
if (line_read && *line_read) {
|
||||
add_history(line_read);
|
||||
}
|
||||
|
||||
return line_read;
|
||||
}
|
||||
|
||||
static int cmd_c(char *args) {
|
||||
cpu_exec(-1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int cmd_q(char *args) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int cmd_help(char *args);
|
||||
|
||||
static struct {
|
||||
const char *name;
|
||||
const char *description;
|
||||
int (*handler) (char *);
|
||||
} cmd_table [] = {
|
||||
{ "help", "Display information about all supported commands", cmd_help },
|
||||
{ "c", "Continue the execution of the program", cmd_c },
|
||||
{ "q", "Exit NEMU", cmd_q },
|
||||
|
||||
/* TODO: Add more commands */
|
||||
|
||||
};
|
||||
|
||||
#define NR_CMD ARRLEN(cmd_table)
|
||||
|
||||
static int cmd_help(char *args) {
|
||||
/* extract the first argument */
|
||||
char *arg = strtok(NULL, " ");
|
||||
int i;
|
||||
|
||||
if (arg == NULL) {
|
||||
/* no argument given */
|
||||
for (i = 0; i < NR_CMD; i ++) {
|
||||
printf("%s - %s\n", cmd_table[i].name, cmd_table[i].description);
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (i = 0; i < NR_CMD; i ++) {
|
||||
if (strcmp(arg, cmd_table[i].name) == 0) {
|
||||
printf("%s - %s\n", cmd_table[i].name, cmd_table[i].description);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
printf("Unknown command '%s'\n", arg);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void sdb_set_batch_mode() {
|
||||
is_batch_mode = true;
|
||||
}
|
||||
|
||||
void sdb_mainloop() {
|
||||
if (is_batch_mode) {
|
||||
cmd_c(NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
for (char *str; (str = rl_gets()) != NULL; ) {
|
||||
char *str_end = str + strlen(str);
|
||||
|
||||
/* extract the first token as the command */
|
||||
char *cmd = strtok(str, " ");
|
||||
if (cmd == NULL) { continue; }
|
||||
|
||||
/* treat the remaining string as the arguments,
|
||||
* which may need further parsing
|
||||
*/
|
||||
char *args = cmd + strlen(cmd) + 1;
|
||||
if (args >= str_end) {
|
||||
args = NULL;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_DEVICE
|
||||
extern void sdl_clear_event_queue();
|
||||
sdl_clear_event_queue();
|
||||
#endif
|
||||
|
||||
int i;
|
||||
for (i = 0; i < NR_CMD; i ++) {
|
||||
if (strcmp(cmd, cmd_table[i].name) == 0) {
|
||||
if (cmd_table[i].handler(args) < 0) { return; }
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (i == NR_CMD) { printf("Unknown command '%s'\n", cmd); }
|
||||
}
|
||||
}
|
||||
|
||||
void init_sdb() {
|
||||
/* Compile the regular expressions. */
|
||||
init_regex();
|
||||
|
||||
/* Initialize the watchpoint pool. */
|
||||
init_wp_pool();
|
||||
}
|
23
nemu/src/monitor/sdb/sdb.h
Normal file
23
nemu/src/monitor/sdb/sdb.h
Normal file
|
@ -0,0 +1,23 @@
|
|||
/***************************************************************************************
|
||||
* Copyright (c) 2014-2022 Zihao Yu, Nanjing University
|
||||
*
|
||||
* NEMU is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* See the Mulan PSL v2 for more details.
|
||||
***************************************************************************************/
|
||||
|
||||
#ifndef __SDB_H__
|
||||
#define __SDB_H__
|
||||
|
||||
#include <common.h>
|
||||
|
||||
word_t expr(char *e, bool *success);
|
||||
|
||||
#endif
|
43
nemu/src/monitor/sdb/watchpoint.c
Normal file
43
nemu/src/monitor/sdb/watchpoint.c
Normal file
|
@ -0,0 +1,43 @@
|
|||
/***************************************************************************************
|
||||
* Copyright (c) 2014-2022 Zihao Yu, Nanjing University
|
||||
*
|
||||
* NEMU is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* See the Mulan PSL v2 for more details.
|
||||
***************************************************************************************/
|
||||
|
||||
#include "sdb.h"
|
||||
|
||||
#define NR_WP 32
|
||||
|
||||
typedef struct watchpoint {
|
||||
int NO;
|
||||
struct watchpoint *next;
|
||||
|
||||
/* TODO: Add more members if necessary */
|
||||
|
||||
} WP;
|
||||
|
||||
static WP wp_pool[NR_WP] = {};
|
||||
static WP *head = NULL, *free_ = NULL;
|
||||
|
||||
void init_wp_pool() {
|
||||
int i;
|
||||
for (i = 0; i < NR_WP; i ++) {
|
||||
wp_pool[i].NO = i;
|
||||
wp_pool[i].next = (i == NR_WP - 1 ? NULL : &wp_pool[i + 1]);
|
||||
}
|
||||
|
||||
head = NULL;
|
||||
free_ = wp_pool;
|
||||
}
|
||||
|
||||
/* TODO: Implement the functionality of watchpoint */
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue