aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Waterman <waterman@cs.berkeley.edu>2016-04-06 11:00:56 -0700
committerAndrew Waterman <waterman@cs.berkeley.edu>2016-04-06 11:01:16 -0700
commitee00503ebecb5f705c8d36c0325ae12afb66a10c (patch)
tree7f557de194ff83899d7d98ba36f7a1418bff7af4
parent0eface07b9c2a565b2fcf95fff3fc755811bfb4e (diff)
downloadpk-ee00503ebecb5f705c8d36c0325ae12afb66a10c.zip
pk-ee00503ebecb5f705c8d36c0325ae12afb66a10c.tar.gz
pk-ee00503ebecb5f705c8d36c0325ae12afb66a10c.tar.bz2
Don't assume existence of uarch counters
-rw-r--r--machine/encoding.h34
-rw-r--r--pk/handlers.c7
-rw-r--r--pk/pk.c57
-rw-r--r--pk/pk.h6
-rw-r--r--pk/syscall.c26
5 files changed, 46 insertions, 84 deletions
diff --git a/machine/encoding.h b/machine/encoding.h
index 6139bb0..c57804d 100644
--- a/machine/encoding.h
+++ b/machine/encoding.h
@@ -629,23 +629,6 @@
#define CSR_CYCLE 0xc00
#define CSR_TIME 0xc01
#define CSR_INSTRET 0xc02
-#define CSR_STATS 0xc0
-#define CSR_UARCH0 0xcc0
-#define CSR_UARCH1 0xcc1
-#define CSR_UARCH2 0xcc2
-#define CSR_UARCH3 0xcc3
-#define CSR_UARCH4 0xcc4
-#define CSR_UARCH5 0xcc5
-#define CSR_UARCH6 0xcc6
-#define CSR_UARCH7 0xcc7
-#define CSR_UARCH8 0xcc8
-#define CSR_UARCH9 0xcc9
-#define CSR_UARCH10 0xcca
-#define CSR_UARCH11 0xccb
-#define CSR_UARCH12 0xccc
-#define CSR_UARCH13 0xccd
-#define CSR_UARCH14 0xcce
-#define CSR_UARCH15 0xccf
#define CSR_SSTATUS 0x100
#define CSR_SIE 0x104
#define CSR_STVEC 0x105
@@ -951,23 +934,6 @@ DECLARE_CSR(fcsr, CSR_FCSR)
DECLARE_CSR(cycle, CSR_CYCLE)
DECLARE_CSR(time, CSR_TIME)
DECLARE_CSR(instret, CSR_INSTRET)
-DECLARE_CSR(stats, CSR_STATS)
-DECLARE_CSR(uarch0, CSR_UARCH0)
-DECLARE_CSR(uarch1, CSR_UARCH1)
-DECLARE_CSR(uarch2, CSR_UARCH2)
-DECLARE_CSR(uarch3, CSR_UARCH3)
-DECLARE_CSR(uarch4, CSR_UARCH4)
-DECLARE_CSR(uarch5, CSR_UARCH5)
-DECLARE_CSR(uarch6, CSR_UARCH6)
-DECLARE_CSR(uarch7, CSR_UARCH7)
-DECLARE_CSR(uarch8, CSR_UARCH8)
-DECLARE_CSR(uarch9, CSR_UARCH9)
-DECLARE_CSR(uarch10, CSR_UARCH10)
-DECLARE_CSR(uarch11, CSR_UARCH11)
-DECLARE_CSR(uarch12, CSR_UARCH12)
-DECLARE_CSR(uarch13, CSR_UARCH13)
-DECLARE_CSR(uarch14, CSR_UARCH14)
-DECLARE_CSR(uarch15, CSR_UARCH15)
DECLARE_CSR(sstatus, CSR_SSTATUS)
DECLARE_CSR(sie, CSR_SIE)
DECLARE_CSR(stvec, CSR_STVEC)
diff --git a/pk/handlers.c b/pk/handlers.c
index 1961852..71466b8 100644
--- a/pk/handlers.c
+++ b/pk/handlers.c
@@ -14,6 +14,13 @@ static void handle_illegal_instruction(trapframe_t* tf)
else
kassert(len == 2);
+ // supply 0 for unimplemented uarch counters
+ if ((tf->insn & (MASK_CSRRS | 0xcc0U<<20)) == (MATCH_CSRRS | 0xcc0U<<20)) {
+ tf->gpr[(tf->insn >> 7) & 0x1f] = 0;
+ tf->epc += 4;
+ return;
+ }
+
dump_tf(tf);
panic("An illegal instruction was executed!");
}
diff --git a/pk/pk.c b/pk/pk.c
index 2737048..6878811 100644
--- a/pk/pk.c
+++ b/pk/pk.c
@@ -4,12 +4,45 @@
#include "elf.h"
#include "mtrap.h"
#include "frontend.h"
+#include <stdbool.h>
elf_info current;
-int uarch_counters_enabled;
-long uarch_counters[NUM_COUNTERS];
-char* uarch_counter_names[NUM_COUNTERS];
+#define NUM_COUNTERS 18
+static int uarch_counters_enabled;
+static long uarch_counters[NUM_COUNTERS];
+static char* uarch_counter_names[NUM_COUNTERS];
+
+static void read_uarch_counters(bool dump)
+{
+ if (!uarch_counters_enabled)
+ return;
+
+ size_t i = 0;
+ #define READ_CTR(name) do { \
+ while (i >= NUM_COUNTERS) ; \
+ long csr = read_csr(name); \
+ if (dump && csr) printk("%s = %ld\n", #name, csr - uarch_counters[i]); \
+ uarch_counters[i++] = csr; \
+ } while (0)
+ READ_CTR(0xcc0); READ_CTR(0xcc1); READ_CTR(0xcc2);
+ READ_CTR(0xcc3); READ_CTR(0xcc4); READ_CTR(0xcc5);
+ READ_CTR(0xcc6); READ_CTR(0xcc7); READ_CTR(0xcc8);
+ READ_CTR(0xcc9); READ_CTR(0xcca); READ_CTR(0xccb);
+ READ_CTR(0xccc); READ_CTR(0xccd); READ_CTR(0xcce);
+ READ_CTR(0xccf); READ_CTR(cycle); READ_CTR(instret);
+ #undef READ_CTR
+}
+
+static void start_uarch_counters()
+{
+ read_uarch_counters(false);
+}
+
+void dump_uarch_counters()
+{
+ read_uarch_counters(true);
+}
static void handle_option(const char* s)
{
@@ -20,7 +53,6 @@ static void handle_option(const char* s)
break;
case 'c': // print uarch counters upon termination
- // If your HW doesn't support uarch counters, then don't use this flag!
uarch_counters_enabled = 1;
break;
@@ -116,22 +148,7 @@ static void run_loaded_program(size_t argc, char** argv, uintptr_t kstack_top)
if (current.t0) // start timer if so requested
current.t0 = rdcycle();
- if (uarch_counters_enabled) { // start tracking the uarch counters if requested
- size_t i = 0;
- #define READ_CTR_INIT(name) do { \
- while (i >= NUM_COUNTERS) ; \
- long csr = read_csr(name); \
- uarch_counters[i++] = csr; \
- } while (0)
- READ_CTR_INIT(cycle); READ_CTR_INIT(instret);
- READ_CTR_INIT(uarch0); READ_CTR_INIT(uarch1); READ_CTR_INIT(uarch2);
- READ_CTR_INIT(uarch3); READ_CTR_INIT(uarch4); READ_CTR_INIT(uarch5);
- READ_CTR_INIT(uarch6); READ_CTR_INIT(uarch7); READ_CTR_INIT(uarch8);
- READ_CTR_INIT(uarch9); READ_CTR_INIT(uarch10); READ_CTR_INIT(uarch11);
- READ_CTR_INIT(uarch12); READ_CTR_INIT(uarch13); READ_CTR_INIT(uarch14);
- READ_CTR_INIT(uarch15);
- #undef READ_CTR_INIT
- }
+ start_uarch_counters();
trapframe_t tf;
init_tf(&tf, current.entry, stack_top);
diff --git a/pk/pk.h b/pk/pk.h
index c86d596..2c68c93 100644
--- a/pk/pk.h
+++ b/pk/pk.h
@@ -35,6 +35,7 @@ int vsnprintf(char* out, size_t n, const char* s, va_list vl);
int snprintf(char* out, size_t n, const char* s, ...);
void start_user(trapframe_t* tf) __attribute__((noreturn));
void dump_tf(trapframe_t*);
+void dump_uarch_counters();
static inline int insn_len(long insn)
{
@@ -43,11 +44,6 @@ static inline int insn_len(long insn)
#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
-#define NUM_COUNTERS (18)
-extern int uarch_counters_enabled;
-extern long uarch_counters[NUM_COUNTERS];
-extern char* uarch_counter_names[NUM_COUNTERS];
-
#ifdef __cplusplus
}
#endif
diff --git a/pk/syscall.c b/pk/syscall.c
index de11a0a..66c07ef 100644
--- a/pk/syscall.c
+++ b/pk/syscall.c
@@ -17,31 +17,7 @@ void sys_exit(int code)
{
if (current.t0)
printk("%ld cycles\n", rdcycle() - current.t0);
-
- if (uarch_counters_enabled) {
- size_t i = 0;
- #define READ_CTR_FINI(name) do { \
- while (i >= NUM_COUNTERS) ; \
- long csr = read_csr(name); \
- csr -= uarch_counters[i]; uarch_counter_names[i] = #name; \
- uarch_counters[i++] = csr; \
- } while (0)
- READ_CTR_FINI(cycle); READ_CTR_FINI(instret);
- READ_CTR_FINI(uarch0); READ_CTR_FINI(uarch1); READ_CTR_FINI(uarch2);
- READ_CTR_FINI(uarch3); READ_CTR_FINI(uarch4); READ_CTR_FINI(uarch5);
- READ_CTR_FINI(uarch6); READ_CTR_FINI(uarch7); READ_CTR_FINI(uarch8);
- READ_CTR_FINI(uarch9); READ_CTR_FINI(uarch10); READ_CTR_FINI(uarch11);
- READ_CTR_FINI(uarch12); READ_CTR_FINI(uarch13); READ_CTR_FINI(uarch14);
- READ_CTR_FINI(uarch15);
- #undef READ_CTR_FINI
-
- for (int i = 0; i < NUM_COUNTERS; i++) {
- if (uarch_counters[i]) {
- printk("%s = %ld\n", uarch_counter_names[i], uarch_counters[i]);
- }
- }
- }
-
+ dump_uarch_counters();
shutdown(code);
}