Skip to content

Commit e8b3853

Browse files
committed
yukon: add more hardware support
Mainly, invalidations
1 parent 19af56a commit e8b3853

File tree

3 files changed

+105
-45
lines changed

3 files changed

+105
-45
lines changed

runtime/include/alaska/alaska.hpp

+18-13
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,18 @@
1919
#include <alaska/list_head.h>
2020
#include <alaska/liballoc.h>
2121
#include <alaska/config.h>
22+
#include <alaska/Logger.hpp>
2223

2324
#include <ck/utility.h>
2425

2526

2627

28+
#include <fcntl.h>
29+
#include <unistd.h>
30+
#include <string.h>
31+
static void show_string(const char *msg) { write(1, msg, strlen(msg)); }
32+
33+
2734
#define HANDLE_ADDRSPACE __attribute__((address_space(1)))
2835

2936
// Fwd decl stuff
@@ -61,26 +68,23 @@ namespace alaska {
6168
};
6269

6370
public:
64-
// Return the pointer. If it is free, return NULL
6571
ALASKA_INLINE void *get_pointer(void) const {
66-
#ifdef ALASKA_SWAP_SUPPORT
67-
// If swapping is enabled, the top bit will be set, so we need to check that
68-
if (unlikely(alt.swap)) {
69-
// Ask the runtime to "swap" the object back in. We blindly assume that
70-
// this will succeed for performance reasons.
71-
return alaska_ensure_present(this);
72-
}
72+
return (void*)(uint64_t)alt.misc;
73+
// return ptr;
74+
}
75+
76+
inline void invalidate(void) {
77+
#ifdef __riscv
78+
__asm__ volatile("csrw 0xc4, %0" ::"rK"((uint64_t)handle_id()) : "memory");
79+
__asm__ volatile("fence" ::: "memory");
7380
#endif
74-
return ptr;
7581
}
7682

7783
void set_pointer(void *ptr) {
7884
reset();
7985
this->ptr = ptr;
8086
alt.invl = 0;
81-
#ifdef __riscv
82-
__asm__ volatile("fence" ::: "memory");
83-
#endif
87+
invalidate();
8488
}
8589

8690

@@ -110,6 +114,7 @@ namespace alaska {
110114
ptr = NULL;
111115
alt.invl = 0;
112116
alt.swap = 0;
117+
invalidate();
113118
}
114119

115120

@@ -136,7 +141,7 @@ namespace alaska {
136141
}
137142

138143
static void *translate(void *handle) {
139-
return alaska::Mapping::from_handle(handle)->get_pointer();
144+
return alaska::Mapping::from_handle(handle)->ptr;
140145
}
141146

142147
// Extract an encoded mapping out of the bits of a handle. WARNING: this function does not

runtime/yukon/yukon.cpp

+86-32
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <alaska/utils.h>
1616
#include <alaska/Runtime.hpp>
1717
#include <alaska/Configuration.hpp>
18+
#include "alaska/HugeObjectAllocator.hpp"
1819
#include <alaska/liballoc.h>
1920
#include <stdlib.h>
2021
#include <errno.h>
@@ -35,7 +36,7 @@
3536
static alaska::ThreadCache *tc = NULL;
3637

3738
static void set_ht_addr(void *addr) {
38-
// alaska::printf("set htaddr to %p\n", addr);
39+
alaska::printf("set htbase to %p\n", addr);
3940
write_csr(0xc2, addr);
4041
}
4142

@@ -72,36 +73,83 @@ static char stdout_buf[BUFSIZ];
7273
static char stderr_buf[BUFSIZ];
7374

7475

76+
77+
static void wait_for_csr_zero(void) {
78+
volatile uint32_t csr_value = 0x1;
79+
do {
80+
__asm__ volatile("csrr %0, 0xc3\n\t" : "=r"(csr_value) : : "memory");
81+
} while (csr_value != 0);
82+
}
83+
84+
85+
#define BACKTRACE_SIZE 100
86+
87+
88+
extern "C" void abort(void) {
89+
void *addr = __builtin_extract_return_addr(__builtin_return_address(0));
90+
91+
printf("Abort from %p!\n", addr);
92+
printf("going to sleep!\n");
93+
94+
sleep(5000000);
95+
exit(-1);
96+
}
97+
98+
99+
static void handle_sig(int sig) {
100+
printf("Caught signal %d (%s)\n", sig, strsignal(sig));
101+
void *buffer[BACKTRACE_SIZE];
102+
// Get the backtrace
103+
int nptrs = backtrace(buffer, BACKTRACE_SIZE);
104+
// Print the backtrace symbols
105+
backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO);
106+
exit(0);
107+
}
108+
109+
110+
75111
static void init(void) {
76112
setvbuf(stdout, stdout_buf, _IOLBF, BUFSIZ);
77113
setvbuf(stderr, stderr_buf, _IOLBF, BUFSIZ);
78114

79115
alaska::Configuration config;
116+
// Use the "malloc" backend to operate cleanly w/ libc's malloc
117+
config.huge_strategy = alaska::HugeAllocationStrategy::MALLOC_BACKED;
80118

81119
the_runtime = new alaska::Runtime(config);
82120
void *handle_table_base = the_runtime->handle_table.get_base();
83121
// printf("Handle table at %p\n", handle_table_base);
84-
set_ht_addr(handle_table_base);
85122
// Make sure the handle table performs mlocks
86123
the_runtime->handle_table.enable_mlock();
124+
125+
asm volatile("fence" ::: "memory");
126+
set_ht_addr(handle_table_base);
127+
128+
129+
signal(SIGABRT, handle_sig);
130+
signal(SIGSEGV, handle_sig);
131+
signal(SIGBUS, handle_sig);
132+
signal(SIGILL, handle_sig);
87133
}
88134

89135

90136

91137

92138
static alaska::ThreadCache *get_tc() {
93-
if (the_runtime == NULL) {
94-
init();
95-
}
96-
if (tc == NULL) {
97-
tc = the_runtime->new_threadcache();
98-
}
139+
if (the_runtime == NULL) init();
140+
if (tc == NULL) tc = the_runtime->new_threadcache();
99141
return tc;
100142
}
101143

102144
void __attribute__((constructor(102))) alaska_init(void) {
103-
unsetenv("LD_PRELOAD"); // make it so we don't run alaska in subprocesses!
145+
unsetenv("LD_PRELOAD"); // make it so we don't run alaska in subprocesses!
104146
get_tc();
147+
148+
atexit([]() {
149+
printf("Setting ht addr to zero!\n");
150+
set_ht_addr(0);
151+
printf("set!\n");
152+
});
105153
}
106154

107155
void __attribute__((destructor)) alaska_deinit(void) {
@@ -111,48 +159,43 @@ void __attribute__((destructor)) alaska_deinit(void) {
111159
// }
112160
// delete the_runtime;
113161
// }
114-
// set_ht_addr(NULL);
162+
// set_ht_addr(0);
115163
}
116164

117-
118-
// #define malloc halloc
119-
// #define calloc hcalloc
120-
// #define realloc hrealloc
121-
// #define free hfree
122-
123165
static void *_halloc(size_t sz, int zero) {
124-
// print_hex("_halloc", sz);
125-
if (dead) {
126-
return alaska_internal_malloc(sz);
127-
}
128-
void *result = get_tc()->halloc(sz, zero);
166+
void *result = NULL;
129167

130-
// This seems right...
168+
result = get_tc()->halloc(sz, zero);
169+
auto m = (uintptr_t)alaska::Mapping::translate(result);
170+
printf("halloc(%zu) -> %p (%zx-%zx)\n", sz, result, m, m + sz);
131171
if (result == NULL) errno = ENOMEM;
172+
132173
return result;
133174
}
134175

135176

136-
extern "C" void *malloc(size_t sz) noexcept { return _halloc(sz, 0); }
177+
// #define halloc malloc
178+
// #define hcalloc calloc
179+
// #define hrealloc realloc
180+
// #define hfree free
181+
182+
extern "C" void *halloc(size_t sz) noexcept { return _halloc(sz, 0); }
137183

138-
extern "C" void *calloc(size_t nmemb, size_t size) { return _halloc(nmemb * size, 1); }
184+
extern "C" void *hcalloc(size_t nmemb, size_t size) { return _halloc(nmemb * size, 1); }
139185

140186
// Reallocate a handle
141-
extern "C" void *realloc(void *handle, size_t new_size) {
187+
extern "C" void *hrealloc(void *handle, size_t new_size) {
142188
auto *tc = get_tc();
143189

144-
// print_hex("realloc", (uint64_t)handle);
145-
// print_hex(" newsz", (uint64_t)new_size);
146190
// If the handle is null, then this call is equivalent to malloc(size)
147191
if (handle == NULL) {
148192
return malloc(new_size);
149193
}
150194
auto *m = alaska::Mapping::from_handle_safe(handle);
151195
if (m == NULL) {
152196
if (!alaska::Runtime::get().heap.huge_allocator.owns(handle)) {
153-
log_debug("realloc edge case: not a handle %p!", handle);
197+
log_fatal("realloc edge case: not a handle %p!", handle);
154198
exit(-1);
155-
// return ::realloc(handle, new_size);
156199
}
157200
}
158201

@@ -170,8 +213,8 @@ extern "C" void *realloc(void *handle, size_t new_size) {
170213

171214

172215

173-
extern "C" void free(void *ptr) {
174-
// print_hex("free", (uint64_t)ptr);
216+
extern "C" void hfree(void *ptr) {
217+
printf("hfree %p\n", ptr);
175218
// no-op if NULL is passed
176219
if (unlikely(ptr == NULL)) return;
177220

@@ -180,4 +223,15 @@ extern "C" void free(void *ptr) {
180223
}
181224

182225

183-
extern "C" size_t malloc_usable_size(void *ptr) { return get_tc()->get_size(ptr); }
226+
extern "C" size_t halloc_usable_size(void *ptr) { return get_tc()->get_size(ptr); }
227+
228+
229+
230+
void *operator new(size_t size) { return halloc(size); }
231+
232+
void *operator new[](size_t size) { return halloc(size); }
233+
234+
235+
void operator delete(void *ptr) { hfree(ptr); }
236+
237+
void operator delete[](void *ptr) { hfree(ptr); }

tools/build_yukon.sh

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ cmake ../ \
1616
-DALASKA_ENABLE_TESTING=OFF \
1717
-DALASKA_CORE_ONLY=ON \
1818
-DALASKA_YUKON=ON \
19+
-DCMAKE_BUILD_TYPE=Release \
1920
-DALASKA_SIZE_BITS=32 \
2021
-DCMAKE_SYSROOT=$ROOT/sysroot
2122

0 commit comments

Comments
 (0)