From ef2b77be011e2b4b415d1c5bb4901431d81dfaee Mon Sep 17 00:00:00 2001 From: Kimplul Date: Wed, 25 Sep 2024 23:13:20 +0300 Subject: somewhat working grid + Starvation is still an issue, but adding a slight bit of randomness seems to help a bit. Also, simulating massive systems like 64x64 is a bit too slow, so scale grid example down to 16x16. 256 cores is still a fair bit, but I might try to think of some optimization methods. I wonder at what point threading becomes faster than just running on a single core? + Test now also sets processor nodes to sleep to avoid starvation issue, seems like an opportunity for a DoS attack if someone can get enough cores to bombard a single node with enough traffic. The riscv64 processor has an initial control interface, very messy code, should probably come up with a more elegant way to deal with sleep/wakeups --- src/cpu/riscv/simple_riscv64.c | 47 ++++++++++++++++++++++++++++++++++++++ src/grid/node.c | 3 +++ src/uart/simple_uart.c | 1 - tests/simple_grid/sim.c | 52 +++++++++++++++++++++--------------------- tests/simple_grid/test.c | 28 +++++++++++------------ 5 files changed, 89 insertions(+), 42 deletions(-) diff --git a/src/cpu/riscv/simple_riscv64.c b/src/cpu/riscv/simple_riscv64.c index 1c4e810..80ee7c1 100644 --- a/src/cpu/riscv/simple_riscv64.c +++ b/src/cpu/riscv/simple_riscv64.c @@ -25,15 +25,19 @@ struct simple_riscv64 { struct component *imem; struct component *dmem; + struct component *ctrl; struct ldst dls; struct ldst ils; + struct ldst cls; uint64_t rcv; /* have to be careful with x0 */ uint64_t regs[32]; uint64_t pc; + + bool sleep; }; /* big endian format, going from smallest to highest address. @@ -537,14 +541,57 @@ static stat simple_riscv64_receive(struct simple_riscv64 *cpu, struct component cpu->ils.state = LDST_DONE; return OK; } + else if (pkt.to == cpu->rcv + 128) { + assert(packet_convsize(&pkt) == 8); + if (cpu->cls.state == LDST_BLOCKED) + return EBUSY; + + uint64_t v = packet_convu64(&pkt); + if (v == 0) { + /* sleep */ + cpu->sleep = true; + } + else if (v == 1) { + /* wake */ + cpu->sleep = false; + } + else { + error("illegal control %s\n", cpu->component.name); + return EBUS; + } + + pkt = response(pkt); + + cpu->ctrl = from; + cpu->cls = (struct ldst){pkt, LDST_IDLE, 0, false}; + stat ret = SEND(cpu, cpu->ctrl, pkt); + if (ret == EBUSY) + cpu->cls.state = LDST_BLOCKED; + + return OK; + } else { error("illegal receive on %s", cpu->component.name); return EBUS; } + + return OK; } static stat simple_riscv64_clock(struct simple_riscv64 *cpu) { + if (cpu->cls.state != LDST_IDLE) { + assert(cpu->cls.state == LDST_BLOCKED); + stat r = SEND(cpu, cpu->ctrl, cpu->cls.pkt); + if (r == EBUSY) + return OK; + + cpu->cls.state = LDST_IDLE; + } + + if (cpu->sleep) + return OK; + stat ret = OK; /* there's an active data transfer we should handle */ diff --git a/src/grid/node.c b/src/grid/node.c index 16cce1b..df237c7 100644 --- a/src/grid/node.c +++ b/src/grid/node.c @@ -56,6 +56,9 @@ static stat port_receive(struct port *port, struct packet pkt) static stat grid_receive(struct grid_node *grid, struct component *from, struct packet pkt) { + if (rand() % 256 == 0) + return EBUSY; + uint16_t x; uint16_t y; uint64_t addr = pkt.to; diff --git a/src/uart/simple_uart.c b/src/uart/simple_uart.c index e640ae1..1528a70 100644 --- a/src/uart/simple_uart.c +++ b/src/uart/simple_uart.c @@ -38,7 +38,6 @@ static stat simple_uart_receive(struct simple_uart *uart, struct component *from } putchar(packet_convu8(&pkt)); - fflush(stdout); set_flags(&uart->pkt, PACKET_DONE); return OK; } diff --git a/tests/simple_grid/sim.c b/tests/simple_grid/sim.c index 519599e..87250c8 100644 --- a/tests/simple_grid/sim.c +++ b/tests/simple_grid/sim.c @@ -11,30 +11,30 @@ #include unsigned char _tmp_test_bin[] = { - 0x1b, 0x86, 0x05, 0x00, 0x63, 0x18, 0x05, 0x00, 0x93, 0x07, 0x10, 0x00, - 0x1b, 0x86, 0x05, 0x00, 0x63, 0x88, 0xf5, 0x0c, 0x93, 0x16, 0x85, 0x00, - 0x37, 0x17, 0x00, 0x10, 0xb3, 0xe6, 0xb6, 0x00, 0x13, 0x17, 0x47, 0x01, - 0x83, 0x37, 0x07, 0x00, 0xe3, 0x9e, 0xd7, 0xfe, 0x1b, 0x57, 0x35, 0x00, - 0xb7, 0x17, 0x00, 0x00, 0x93, 0x06, 0x80, 0x02, 0x13, 0x77, 0x77, 0x00, - 0x23, 0x80, 0xd7, 0x00, 0x13, 0x07, 0x07, 0x03, 0x93, 0x76, 0x75, 0x00, - 0x23, 0x80, 0xe7, 0x00, 0x13, 0x87, 0x06, 0x03, 0x23, 0x80, 0xe7, 0x00, - 0x93, 0x06, 0xc0, 0x02, 0x1b, 0xd7, 0x35, 0x00, 0x23, 0x80, 0xd7, 0x00, - 0x13, 0x77, 0x77, 0x00, 0x93, 0x06, 0x00, 0x02, 0x23, 0x80, 0xd7, 0x00, - 0x13, 0x07, 0x07, 0x03, 0x93, 0xf6, 0x75, 0x00, 0x23, 0x80, 0xe7, 0x00, - 0x13, 0x87, 0x06, 0x03, 0x23, 0x80, 0xe7, 0x00, 0x13, 0x07, 0x90, 0x02, + 0x93, 0x96, 0x05, 0x03, 0x13, 0x18, 0x05, 0x02, 0x93, 0x86, 0x06, 0x08, + 0xb3, 0xe6, 0x06, 0x01, 0x9b, 0x88, 0x05, 0x00, 0x63, 0x18, 0x05, 0x00, + 0x93, 0x07, 0x20, 0x00, 0x9b, 0x88, 0x05, 0x00, 0x63, 0x84, 0xf5, 0x0c, + 0x23, 0xb0, 0x06, 0x00, 0x1b, 0x57, 0x35, 0x00, 0xb7, 0x17, 0x00, 0x00, + 0x13, 0x06, 0x80, 0x02, 0x13, 0x77, 0x77, 0x00, 0x23, 0x80, 0xc7, 0x00, + 0x13, 0x07, 0x07, 0x03, 0x13, 0x76, 0x75, 0x00, 0x23, 0x80, 0xe7, 0x00, + 0x13, 0x07, 0x06, 0x03, 0x23, 0x80, 0xe7, 0x00, 0x13, 0x06, 0xc0, 0x02, + 0x1b, 0xd7, 0x35, 0x00, 0x23, 0x80, 0xc7, 0x00, 0x13, 0x77, 0x77, 0x00, + 0x13, 0x06, 0x00, 0x02, 0x23, 0x80, 0xc7, 0x00, 0x13, 0x07, 0x07, 0x03, + 0x13, 0xf6, 0x75, 0x00, 0x23, 0x80, 0xe7, 0x00, 0x13, 0x07, 0x06, 0x03, + 0x23, 0x80, 0xe7, 0x00, 0x13, 0x07, 0x90, 0x02, 0x23, 0x80, 0xe7, 0x00, + 0x13, 0x07, 0xa0, 0x00, 0x23, 0x80, 0xe7, 0x00, 0x93, 0x07, 0xf0, 0x00, + 0x1b, 0x07, 0x05, 0x00, 0x63, 0x04, 0xf5, 0x02, 0x63, 0x86, 0xf8, 0x02, + 0x9b, 0x85, 0x15, 0x00, 0x93, 0x95, 0x05, 0x03, 0x93, 0x85, 0x05, 0x08, + 0x33, 0x68, 0xb8, 0x00, 0x93, 0x07, 0x10, 0x00, 0x23, 0x30, 0xf8, 0x00, + 0x23, 0xb0, 0x06, 0x00, 0x67, 0x80, 0x00, 0x00, 0xe3, 0x90, 0xe8, 0xfe, + 0x73, 0x00, 0x10, 0x00, 0x1b, 0x05, 0x15, 0x00, 0x13, 0x15, 0x05, 0x03, + 0x13, 0x58, 0x05, 0x01, 0x93, 0x05, 0x00, 0x08, 0x33, 0x68, 0xb8, 0x00, + 0x93, 0x07, 0x10, 0x00, 0x23, 0x30, 0xf8, 0x00, 0x23, 0xb0, 0x06, 0x00, + 0x67, 0x80, 0x00, 0x00, 0xb7, 0x17, 0x00, 0x00, 0x13, 0x07, 0x00, 0x03, 0x23, 0x80, 0xe7, 0x00, 0x13, 0x07, 0xa0, 0x00, 0x23, 0x80, 0xe7, 0x00, - 0x93, 0x07, 0xf0, 0x03, 0x63, 0x0c, 0xf6, 0x02, 0x9b, 0x85, 0x15, 0x00, - 0x93, 0x95, 0x05, 0x03, 0x93, 0xd5, 0x05, 0x03, 0x13, 0x15, 0x85, 0x00, - 0x37, 0x17, 0x00, 0x10, 0x33, 0x65, 0xb5, 0x00, 0x13, 0x17, 0x47, 0x01, - 0x23, 0x30, 0xa7, 0x00, 0xb7, 0x46, 0x00, 0x00, 0x83, 0x37, 0x07, 0x00, - 0xe3, 0x9e, 0xd7, 0xfe, 0x73, 0x00, 0x10, 0x00, 0x67, 0x80, 0x00, 0x00, - 0x1b, 0x05, 0x15, 0x00, 0x13, 0x15, 0x05, 0x03, 0x13, 0x55, 0x05, 0x03, - 0x93, 0x05, 0x00, 0x00, 0x6f, 0xf0, 0x9f, 0xfc, 0xb7, 0x17, 0x00, 0x10, - 0x93, 0x97, 0x47, 0x01, 0x23, 0xb0, 0x07, 0x00, 0x13, 0x07, 0x00, 0x03, - 0xb7, 0x17, 0x00, 0x00, 0x23, 0x80, 0xe7, 0x00, 0x13, 0x07, 0xa0, 0x00, - 0x23, 0x80, 0xe7, 0x00, 0x6f, 0xf0, 0xdf, 0xf2 + 0x6f, 0xf0, 0xdf, 0xf2 }; -unsigned int _tmp_test_bin_len = 260; +unsigned int _tmp_test_bin_len = 256; static struct component *get_grid(struct component **grid, int i, int j, uint8_t x, uint8_t y) { @@ -70,7 +70,7 @@ static stat build_grid(struct clock_domain *clk, uint8_t x, uint8_t y) if (i == 0 && j == 0) continue; - if (i == 1 && j == 1) + if (i == 0 && j == 1) continue; struct component *imem = create_simple_mem(4096); @@ -93,14 +93,14 @@ static stat build_grid(struct clock_domain *clk, uint8_t x, uint8_t y) struct component *dmem = create_simple_mem(4096); clock_domain_add(clk, dmem); - grid_node_connect(grid[x + 1], grid[1], grid[2 * x + 1], grid[x + 2], grid[x], dmem); + grid_node_connect(grid[1], NULL, grid[x + 1], grid[2], grid[0], dmem); for (int i = 0; i < x; ++i) for (int j = 0; j < y; ++j) { if (i == 0 && j == 0) continue; - if (i == 1 && j == 1) + if (i == 0 && j == 1) continue; struct component *node = grid[i * x + j]; @@ -121,7 +121,7 @@ int main() { struct clock_domain *clk = create_clock_domain(NS(1)); - stat r = build_grid(clk, 64, 64); + stat r = build_grid(clk, 16, 16); assert(r == OK); struct gran_root *root = create_root(); diff --git a/tests/simple_grid/test.c b/tests/simple_grid/test.c index 07f1123..2891a6e 100644 --- a/tests/simple_grid/test.c +++ b/tests/simple_grid/test.c @@ -1,23 +1,21 @@ -#define X 64 -#define Y 64 +#define X 16 +#define Y 16 void _start(unsigned short x, unsigned short y) { - volatile unsigned long *counter = (unsigned long *)((1ULL << 48) | (1ULL << 32)); + volatile unsigned long *control = (unsigned long *)(((unsigned long)x << 32) | ((unsigned long)y << 48) + 128); volatile char *uart = (char *)4096; /* very hacky, not recommended but good enough for testing */ - if (x == 0 && y == 1) { - *counter = 0; + if (x == 0 && y == 2) { *uart = '0'; *uart = '\n'; } else - while (*counter != ((x << 8) | y)) - ; + *control = 0; /* go to sleep */ *uart = '('; - /* [0 - 63] as two octal numbers */ + /* [0 - 16] as two octal numbers */ *uart = ((x >> 3) & 0x7) + '0'; *uart = ((x >> 0) & 0x7) + '0'; @@ -30,17 +28,17 @@ void _start(unsigned short x, unsigned short y) *uart = ')'; *uart = '\n'; - if (y == 63) { + if (x == 15 && y == 15) + asm ("ebreak\n"); + + if (y == 15) { x++; y = 0; } else y++; - *counter = (x << 8) | y; - - while (*counter != (64 << 8)) - ; - - asm ("ebreak\n"); + volatile unsigned long *next = (unsigned long *)(((unsigned long)x << 32) | ((unsigned long)y << 48) + 128); + *next = 1; /* wake next core */ + *control = 0; /* go to sleep again */ } -- cgit v1.3