Commit Diff


commit - c2b4813c74b08dda75ed57b11761d7a362bb9404
commit + 6bf04703743693934f0f5a10196d93242b519acc
blob - 8bd6abd55a0641087d858cbd60caa51d2a4d27e1
blob + a152ce5ff7756db87c05a0c99728b86c63a18c0e
--- Makefile
+++ Makefile
@@ -10,11 +10,12 @@ CROSS	= ./tools/bin/${TARGET}
 CFLAGS	= -std=c2x -fPIC -O0 -g
 LDFLAGS	= -pie -static -lpthread
 OBJ	= src/rvemu.o src/ecall.o src/cpu.o src/exec.o
-T	= test
+T	= asm
 PROGS	= examples/test.elf	\
 	  examples/echo.elf	\
 	  examples/cat.elf	\
-	  examples/hello.elf
+	  examples/hello.elf	\
+	  examples/asm.elf
 
 all: rvemu ${PROGS}
 
@@ -51,4 +52,7 @@ src/syscalls.h: src/syscalls.inc
 
 
 .c.elf:
-	${CROSS}-gcc -g -o $@ $< -O2
+	${CROSS}-gcc -g -o $@ $< -Og
+
+.S.elf:
+	${CROSS}-gcc -g -o $@ $<
blob - 59f126f89644bd5a2f80bb454eb876f488386d09
blob + b39eecdefe0f9e9d959459ada0c867b5b9d81afd
--- examples/cat.c
+++ examples/cat.c
@@ -33,8 +33,8 @@ int main (int argc, char* argv[]) {
 
 		cat (file);
 
-		if (file != stdin)
-			fclose (file);
+		//if (file != stdin)
+		//	fclose (file);
 	}
 	return ec;
 }
blob - /dev/null
blob + b028131c30909ef701872a6364405aa0effdea68 (mode 644)
--- /dev/null
+++ examples/asm.S
@@ -0,0 +1,117 @@
+.section .rodata
+fmt:	.asciz "Hello World\n"
+ifmt:	.asciz "%d\n"
+
+.section .data
+arr: .dword 1, 2, 3, 4, 5
+
+.section .text
+.global main
+main:
+	addi sp, sp, -16
+	sd s0, 8(sp)
+	sd ra, 0(sp)
+
+	jal hello
+
+	jal printarr
+	jal nl
+
+	la a0, arr
+	li a1, 1
+	jal swap
+
+	jal printarr
+
+	jal nl
+
+	la a0, arr
+	li a1, 0
+	jal avg
+	mv s0, a0
+	jal print
+
+	mv a0, s0
+	jal even
+	jal print
+
+	mv a0, zero
+	ld ra, 0(sp)
+	ld s0, 8(sp)
+	addi sp, sp, 16
+	ret
+
+// void hello (void);
+hello:
+	la a0, fmt
+	j printf
+
+// void nl (void);
+nl:
+	li a0, 10
+	j putchar
+
+// void print (int);
+print:
+	addi sp, sp, -8
+	sd ra, 0(sp)
+
+	mv a1, a0
+	la a0, ifmt
+	jal printf
+	
+	ld ra, 0(sp)
+	addi sp, sp, 8
+	ret
+
+// void printarr (void);
+printarr:
+	addi sp, sp, -32
+	sd ra, 16(sp)
+	sd s0, 8(sp)
+	sd s1, 0(sp)
+
+	la s0, arr
+	mv s1, zero
+
+ploop:
+	add a0, s0, s1
+	ld a0, 0(a0)
+	jal print
+
+	addi s1, s1, 8
+	li t0, (5 * 8)
+	bne s1, t0, ploop
+
+
+	ld s1, 0(sp)
+	ld s0, 8(sp)
+	ld ra, 16(sp)
+	addi sp, sp, 32
+	ret
+
+// void swap (long *v, size_t n);
+swap:
+	slli a1, a1, 3
+	add t0, a0, a1
+	ld t1, 0(t0)
+	ld t2, 8(t0)
+	sd t2, 0(t0)
+	sd t1, 8(t0)
+	ret
+	
+// long avg(long *v, size_t n);
+avg:
+	slli a1, a1, 3
+	add a0, a0, a1
+	ld t0, 0(a0)
+	ld t1, 8(a0)
+	add a0, t0, t1
+	srli a0, a0, 1
+	ret
+
+// bool even(long);
+even:
+	andi a0, a0, 1
+	xor a0, a0, 1
+	ret
blob - 461d04c28c839ed4785c5de2636e20ba22fea3b6
blob + 55a7a59cd36faa349357bfd385c3c37bd7116c37
--- examples/test.c
+++ examples/test.c
@@ -1,4 +1,6 @@
+#include <sys/mman.h>
 #include <unistd.h>
+#include <stdlib.h>
 #include <string.h>
 #include <fcntl.h>
 #include <stdio.h>
@@ -10,21 +12,25 @@ inline static void ebreak (void)
 	__asm __volatile__ ("ebreak");
 }
 
+__attribute__((always_inline))
+inline static void *sys_brk (void *ptr)
+{
+	register void *a0 asm ("a0") = ptr;
+	register int a7 asm ("a7") = 214;
+	__asm __volatile__ ("ecall" : "=r"(a0) : "r"(a0), "r"(a7));
+	return a0;
+}
+
 int main (int argc, char *argv[]) {
-	FILE *file;
-	char buf[256];
+	volatile int *ptr, *p2;
 
-	file = fopen ("test.txt", "r");
-	if (file == NULL)
-		err (1, "fopen()");
+	ptr = sys_brk (NULL);
+	p2 = sys_brk ((void *)((size_t)ptr + 4096));
+	printf ("XXX ptr = %p, p2 = %p\n", ptr, p2);
 
-	if (fgets (buf, sizeof (buf), file) == NULL)
-		err (1, "fgets()");
+	*ptr = 42;
 
-	if (puts (buf) < 0)
-		err (1, "puts()");
+	printf ("XXX *ptr = %d\n", *ptr);
 
-	if (fclose (file) != 0)
-		err (1, "fclose()");
 	return 0;
 }
blob - 44537fe39bb4e8ea22f90917381b9327ef99beb9
blob + cd04cc43d95dd25d3dafe21a8e5e92fbcd24437f
--- src/cpu.c
+++ src/cpu.c
@@ -373,16 +373,6 @@ void cpu_exec (u32 instr)
 			eprintf ("%08llx: ebreak\n", pc - 4);
 			__asm __volatile__ ("int $3");
 		} else {
-			log (
-				"ecall a0=%llu, a1=%llu, a2=%llu, a3=%llu, a4=%llu, a5=%llu, a7=%llu",
-				cpu_get (10),
-				cpu_get (11),
-				cpu_get (12),
-				cpu_get (13),
-				cpu_get (14),
-				cpu_get (15),
-				cpu_get (17)
-			);
 			ecall ();
 		}
 		break;
blob - 2a2be702a6b8af0ce87e0001d697567fcf2681dc
blob + 10dd1b856b79ac23af9c2869b2eb5b623d697c78
--- src/ecall.c
+++ src/ecall.c
@@ -135,26 +135,27 @@ static int enosys (const char *sys)
 
 static u64 my_brk (u64 new)
 {
+	size_t ps;
 	void *ptr;
 
+	eprintf ("brk(%llx, %llx);", new, brkval);
+
 	if (new < brkval)
 		return brkval;
-	if (new >= 0x78000000) {
-		errno = ENOMEM;
-		return -1;
-	}
 
-	new = (new + 4095) & ~0x3ff;
+	ps = getpagesize ();
+	new = (new + ps - 1) & ~(ps - 1);
+	eprintf ("mmap (%p, %zu);", (void *)brkval, (size_t)(new - brkval));
 	ptr = mmap (
 		(void *)brkval,
 		new - brkval,
 		PROT_READ | PROT_WRITE,
-		MAP_PRIVATE | MAP_ANON,
+		MAP_PRIVATE | MAP_ANON | MAP_FIXED,
 		-1,
 		0
 	);
-	if (ptr == NULL)
-		return -1;
+	if (ptr == MAP_FAILED)
+		return -map_errno (errno);
 	brkval = new;
 	return brkval;
 }
@@ -207,6 +208,13 @@ void ecall (void)
 	const u64 a4 = cpu_get (14);
 	const u64 a5 = cpu_get (15);
 	const u64 a7 = cpu_get (17);
+
+
+	eprintf (
+			"ecall a7=%llu, a0=%llu, a1=%llu, a2=%llu, a3=%llu, a4=%llu, a5=%llu",
+			a7, a0, a1, a2, a3, a4, a5
+	);
+
 	int tmp, tmp2;
 	i64 ret;
 	switch (a7) {
@@ -770,6 +778,7 @@ void ecall (void)
 		tmp2 = mmap_prot ((int)a2);
 		eprintf ("mmap (%p, %zu, %d, %d, %d, %lld);\n", ptr (void, a0), (size_t)a1, tmp2, tmp, (int)a4, (off_t)a5);
 		ptr = mmap (ptr (void, a0), (size_t)a1, tmp2, tmp, (int)a4, (off_t)a5);
+		printf ("ptr = %p\n", ptr);
 		if (ptr == NULL) {
 			ret = -map_errno (errno);
 		} else {
@@ -786,7 +795,8 @@ void ecall (void)
 		ret = enosys ("swapoff");
 		break;
 	case SYS_mprotect:
-		ret = map (mprotect (ptr (void, a0), (size_t)a1, (int)a2));
+		tmp = mmap_prot ((int)a2);
+		ret = map (mprotect (ptr (void, a0), (size_t)a1, tmp));
 		break;
 	case SYS_msync:
 		ret = map (msync (ptr (void, a0), (size_t)a1, (int)a2));
blob - 3f16fad3949a076ddca5b6547797a5a8685800f8
blob + 5854ca099f96147330e611adc8d81f8150914174
--- src/rvemu.c
+++ src/rvemu.c
@@ -50,7 +50,7 @@ static void load_segment (int fd, Elf64_Phdr phdr)
 	if (mprotect (addr, len, prot) != 0)
 		err (1, "mprotect()");
 
-	end = phdr.p_vaddr + phdr.p_memsz;
+	end = (phdr.p_vaddr + phdr.p_memsz + ps - 1) & ~(ps - 1);
 	if (end > brkval)
 		brkval = end;
 }