parent
5b9d0b55ea
commit
c4427c64d2
@ -0,0 +1,20 @@
|
||||
#ifndef _SYNC_UTILS_H_
|
||||
#define _SYNC_UTILS_H_
|
||||
|
||||
static inline void sync_barrier(volatile int *counter, int all) {
|
||||
|
||||
int local;
|
||||
|
||||
asm volatile("amoadd.w %0, %2, (%1)\n"
|
||||
: "=r"(local)
|
||||
: "r"(counter), "r"(1)
|
||||
: "memory");
|
||||
|
||||
if (local + 1 < all) {
|
||||
do {
|
||||
asm volatile("lw %0, (%1)\n" : "=r"(local) : "r"(counter) : "memory");
|
||||
} while (local < all);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,24 @@
|
||||
#include "user_lib.h"
|
||||
#include "util/types.h"
|
||||
|
||||
#define N 5
|
||||
#define BASE 0
|
||||
|
||||
int main(void) {
|
||||
void *p[N];
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
p[i] = naive_malloc();
|
||||
int *pi = p[i];
|
||||
*pi = BASE + i;
|
||||
printu("=== user alloc 0 @ vaddr 0x%x\n", p[i]);
|
||||
}
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
int *pi = p[i];
|
||||
printu("=== user0: %d\n", *pi);
|
||||
naive_free(p[i]);
|
||||
}
|
||||
|
||||
exit(0);
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
#include "user_lib.h"
|
||||
#include "util/types.h"
|
||||
|
||||
#define N 5
|
||||
#define BASE 5
|
||||
|
||||
int main(void) {
|
||||
void *p[N];
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
p[i] = naive_malloc();
|
||||
int *pi = p[i];
|
||||
*pi = BASE + i;
|
||||
printu(">>> user alloc 1 @ vaddr 0x%x\n", p[i]);
|
||||
}
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
int *pi = p[i];
|
||||
printu(">>> user 1: %d\n", *pi);
|
||||
naive_free(p[i]);
|
||||
}
|
||||
|
||||
exit(0);
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
/*
|
||||
* The application of lab2_3.
|
||||
*/
|
||||
|
||||
#include "user_lib.h"
|
||||
#include "util/types.h"
|
||||
|
||||
//
|
||||
// compute the summation of an arithmetic sequence. for a given "n", compute
|
||||
// result = n + (n-1) + (n-2) + ... + 0
|
||||
// sum_sequence() calls itself recursively till 0. The recursive call, however,
|
||||
// may consume more memory (from stack) than a physical 4KB page, leading to a page fault.
|
||||
// PKE kernel needs to improved to handle such page fault by expanding the stack.
|
||||
//
|
||||
uint64 sum_sequence(uint64 n) {
|
||||
if (n == 0)
|
||||
return 0;
|
||||
else
|
||||
return sum_sequence( n-1 ) + n;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
// we need a large enough "n" to trigger pagefaults in the user stack
|
||||
uint64 n = 1000;
|
||||
|
||||
printu("Summation of an arithmetic sequence from 0 to %ld is: %ld \n", n, sum_sequence(1000) );
|
||||
exit(0);
|
||||
}
|
Loading…
Reference in new issue