Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simulator exit #368

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sw/cheri/checks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(CHECKS
gpio_check.cc
lcd_check.cc
uart_check.cc
uart_simexit_check.cc
sdraw_check.cc
spi_test.cc
system_info_check.cc
Expand Down
40 changes: 40 additions & 0 deletions sw/cheri/checks/uart_simexit_check.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright lowRISC contributors.
* Licensed under the Apache License, Version 2.0, see LICENSE for details.
* SPDX-License-Identifier: Apache-2.0
*/

#define CHERIOT_NO_AMBIENT_MALLOC
#define CHERIOT_NO_NEW_DELETE
#define CHERIOT_PLATFORM_CUSTOM_UART

#include <stdint.h>

// clang-format off
#include "../../common/defs.h"
#include <cheri.hh>
// clang-format on
#include <platform-uart.hh>
#include "../common/uart-utils.hh"

using namespace CHERI;

/**
* C++ entry point for the loader. This is called from assembly, with the
* read-write root in the first argument.
*/
[[noreturn]] extern "C" void entry_point(void* rwRoot) {
Capability<void> root{rwRoot};

// Create a bounded capability to the UART
Capability<volatile OpenTitanUart> uart = root.cast<volatile OpenTitanUart>();
uart.address() = UART_ADDRESS;
uart.bounds() = UART_BOUNDS;

uart->init(BAUD_RATE);
write_str(uart, "Trying out simulation exit!\r\n");
write_str(uart, "Safe to exit simulator.\xd8\xaf\xfb\xa0\xc7\xe1\xa9\xd7");
write_str(uart, "This should not be printed.\r\n");
write_str(uart, "FAIL\r\n");
while (true);
}
2 changes: 1 addition & 1 deletion vendor/lowrisc_ip.lock.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
upstream:
{
url: https://github.com/lowRISC/opentitan
rev: 4fe1b8dd1a09af9dbc242434481ae031955dfd85
rev: a78922f14a8cc20c7ee569f322a04626f2ac6127
}
}
21 changes: 19 additions & 2 deletions vendor/lowrisc_ip/dv/dpi/uartdpi/uartdpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
#include <string.h>
#include <unistd.h>

#define EXIT_MESSAGE_LENGTH (32)

// This keeps the necessary uart state.
struct uartdpi_ctx {
char ptyname[64];
char exitstring[EXIT_MESSAGE_LENGTH];
int exittracker;
int host;
int device;
char tmp_read;
Expand Down Expand Up @@ -84,6 +88,9 @@ void *uartdpi_create(const char *name, const char *log_file_path) {
}
}

ctx->exittracker = 0;
strncpy(ctx->exitstring, "Safe to exit simulator.\xd8\xaf\xfb\xa0\xc7\xe1\xa9\xd7", EXIT_MESSAGE_LENGTH);

return (void *)ctx;
}

Expand Down Expand Up @@ -123,11 +130,12 @@ char uartdpi_read(void *ctx_void) {
return ctx->tmp_read;
}

void uartdpi_write(void *ctx_void, char c) {
// Returns true when simulator should exit.
int uartdpi_write(void *ctx_void, char c) {
int rv;
struct uartdpi_ctx *ctx = (struct uartdpi_ctx *)ctx_void;
if (ctx == NULL) {
return;
return 0;
}

rv = write(ctx->host, &c, 1);
Expand All @@ -143,4 +151,13 @@ void uartdpi_write(void *ctx_void, char c) {
fprintf(stderr, "UART: Write to log file failed: %s\n", strerror(errno));
}
}

if (c == ctx->exitstring[ctx->exittracker]) {
ctx->exittracker++;
} else {
ctx->exittracker = 0;
}

// Don't require 0 to be sent at the end.
return ctx->exittracker == (EXIT_MESSAGE_LENGTH - 1);
}
2 changes: 1 addition & 1 deletion vendor/lowrisc_ip/dv/dpi/uartdpi/uartdpi.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void *uartdpi_create(const char *name, const char *log_file_path);
void uartdpi_close(void *ctx_void);
int uartdpi_can_read(void *ctx_void);
char uartdpi_read(void *ctx_void);
void uartdpi_write(void *ctx_void, char c);
int uartdpi_write(void *ctx_void, char c);

#ifdef __cplusplus
} // extern "C"
Expand Down
7 changes: 5 additions & 2 deletions vendor/lowrisc_ip/dv/dpi/uartdpi/uartdpi.sv
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module uartdpi #(
int uartdpi_can_read(input chandle ctx);

import "DPI-C" function
void uartdpi_write(input chandle ctx, int data);
int uartdpi_write(input chandle ctx, int data);

chandle ctx;
string log_file_path = DEFAULT_LOG_FILE;
Expand Down Expand Up @@ -133,7 +133,10 @@ module uartdpi #(
if (rxcyccount == CYCLES_PER_SYMBOL - 1) begin
rxactive <= 0;
if (rx_i) begin
uartdpi_write(ctx, rxsymbol);
if(uartdpi_write(ctx, rxsymbol)) begin
$display("Exiting the Sonata simulator because the magic UART string was seen.");
$exit;
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
diff --git a/uartdpi.c b/uartdpi.c
index f99b9da3..97366a91 100644
--- a/uartdpi.c
+++ b/uartdpi.c
@@ -19,9 +19,13 @@
#include <string.h>
#include <unistd.h>

+#define EXIT_MESSAGE_LENGTH (32)
+
// This keeps the necessary uart state.
struct uartdpi_ctx {
char ptyname[64];
+ char exitstring[EXIT_MESSAGE_LENGTH];
+ int exittracker;
int host;
int device;
char tmp_read;
@@ -84,6 +88,9 @@ void *uartdpi_create(const char *name, const char *log_file_path) {
}
}

+ ctx->exittracker = 0;
+ strncpy(ctx->exitstring, "Safe to exit simulator.\xd8\xaf\xfb\xa0\xc7\xe1\xa9\xd7", EXIT_MESSAGE_LENGTH);
+
return (void *)ctx;
}

@@ -123,11 +130,12 @@ char uartdpi_read(void *ctx_void) {
return ctx->tmp_read;
}

-void uartdpi_write(void *ctx_void, char c) {
+// Returns true when simulator should exit.
+int uartdpi_write(void *ctx_void, char c) {
int rv;
struct uartdpi_ctx *ctx = (struct uartdpi_ctx *)ctx_void;
if (ctx == NULL) {
- return;
+ return 0;
}

rv = write(ctx->host, &c, 1);
@@ -143,4 +151,13 @@ void uartdpi_write(void *ctx_void, char c) {
fprintf(stderr, "UART: Write to log file failed: %s\n", strerror(errno));
}
}
+
+ if (c == ctx->exitstring[ctx->exittracker]) {
+ ctx->exittracker++;
+ } else {
+ ctx->exittracker = 0;
+ }
+
+ // Don't require 0 to be sent at the end.
+ return ctx->exittracker == (EXIT_MESSAGE_LENGTH - 1);
}
diff --git a/uartdpi.h b/uartdpi.h
index 63834078..5e1152c9 100644
--- a/uartdpi.h
+++ b/uartdpi.h
@@ -13,7 +13,7 @@ void *uartdpi_create(const char *name, const char *log_file_path);
void uartdpi_close(void *ctx_void);
int uartdpi_can_read(void *ctx_void);
char uartdpi_read(void *ctx_void);
-void uartdpi_write(void *ctx_void, char c);
+int uartdpi_write(void *ctx_void, char c);

#ifdef __cplusplus
} // extern "C"
diff --git a/uartdpi.sv b/uartdpi.sv
index d476c519..0d46e79e 100644
--- a/uartdpi.sv
+++ b/uartdpi.sv
@@ -33,7 +33,7 @@ module uartdpi #(
int uartdpi_can_read(input chandle ctx);

import "DPI-C" function
- void uartdpi_write(input chandle ctx, int data);
+ int uartdpi_write(input chandle ctx, int data);

chandle ctx;
string log_file_path = DEFAULT_LOG_FILE;
@@ -133,7 +133,10 @@ module uartdpi #(
if (rxcyccount == CYCLES_PER_SYMBOL - 1) begin
rxactive <= 0;
if (rx_i) begin
- uartdpi_write(ctx, rxsymbol);
+ if(uartdpi_write(ctx, rxsymbol)) begin
+ $display("Exiting the Sonata simulator because the magic UART string was seen.");
+ $exit;
+ end
end
end
end
Loading