hlfw.ca

drawcpu

Download patch

ref: 868b16b5459193afd2044121c09884ccf5560df2
parent: 847cd0b5262ebdf9deb3c294bedffc162b4a80d2
author: halfwit <michaelmisch1985@gmail.com>
date: Fri Sep 6 12:19:36 PDT 2024

Update without start

--- a/include/lib.h
+++ b/include/lib.h
@@ -315,7 +315,6 @@
 extern	char*	utfecpy(char*, char*, char*);
 extern	int	tas(int*);
 extern  int trampoline(void*);
-extern  void start(uintptr_t, Tos *, int, char **);
 extern  long sysintercept(void*, void*, void*, void*, void*, void*, void*);
 extern  int patch(void*, int);
 extern	void	quotefmtinstall(void);
--- a/kern/posix.c
+++ b/kern/posix.c
@@ -35,7 +35,9 @@
 };
 
 static pthread_key_t prdakey;
+typedef void (*startfn)(uintptr, Tos*, int, char**);
 
+
 Proc*
 _getproc(void)
 {
@@ -135,10 +137,12 @@
 	sched_yield();
 }
 
+
 static void*
 trex(void *vp)
 {
 	Proc *p;
+	startfn start;
 	Tos tos;
 	int argc;
 
@@ -147,7 +151,9 @@
 	argc = nelem((char**)p->arg);
 	if(pthread_setspecific(prdakey, p))
 		panic("cannot setspecific");
+	start = (startfn)up->bin->entry;
 	start(up->bin->text, &tos, argc, p->arg);
+	print("Done\n");
 	pexit("", 0);
 	return 0;
 }
@@ -156,11 +162,15 @@
 osexec(Proc *p)
 {
 	pthread_t pid;
-	if(pthread_create(&pid, nil, trex, p)){
+	pthread_attr_t attr;
+	pthread_attr_init(&attr);
+	pthread_attr_setstacksize(&attr, 1024*1024);
+	if(pthread_create(&pid, &attr, trex, p)){
 		oserrstr();
 		panic("osexec: %r");
 	}
 	pthread_join(pid, nil);
+	pthread_attr_destroy(&attr);
 }
 
 void
@@ -181,7 +191,6 @@
 	int n;
 	void *text;
 
-	//text = mmap(nil, up->bin->ts, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
 	text = mallocz(up->bin->ts, 1);
 	n = devtab[tc->type]->read(tc, text, up->bin->ts, 0);
 	if(!text || n == 0)
@@ -193,7 +202,7 @@
 void
 ospatchtext(void)
 {
-	int n, flag;
+	int n;
 	void *text, *final;
 
 	/* Set up trampoline. Mach dependent */
@@ -202,12 +211,7 @@
 	if(n != TRAMPSIZE)
 		error("building trampoline failed");
 
-	flag = MAP_PRIVATE|MAP_ANONYMOUS;
-
-	#ifdef __APPLE__
-	flag |= MAP_JIT;
-	#endif
-	final = mmap(0, up->bin->ts+n, PROT_READ|PROT_WRITE, flag, -1, 0);
+	final = mmap(nil, up->bin->ts+n, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
 	memmove(final, text, n);
 	memmove(final+n, (void*)up->bin->text, up->bin->ts);
 	if(!final)
@@ -214,12 +218,13 @@
 		error("unable to set up text segment with trampoline");
 
 	/* Patch. Mach dependent */
-	if(patch(final+n+1, up->bin->ts) < 0)
+	if(patch(final+n, up->bin->ts) < 0)
 		error("unable to patch syscalls");
 
-	mprotect(final, n + up->bin->ts, PROT_READ|PROT_EXEC);
+	if(mprotect(final, up->bin->ts+n, PROT_READ|PROT_EXEC) != 0)
+		error("Unable to mprotect: %r");
+
 	up->bin->text = (uintptr)final+n;
-	poperror();
 }
 
 void
@@ -228,7 +233,7 @@
 	int n;
 	void *data;
 
-	data = mmap((void*)up->bin->data, up->bin->ds, PROT_READ| PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+	data = mmap((void*)up->bin->data, up->bin->ds, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
 	n = devtab[tc->type]->read(tc, data, up->bin->ds, up->bin->ts);
 	if(!data || n == 0)
 		error("unable to set up data segment");
--- a/kern/sysproc.c
+++ b/kern/sysproc.c
@@ -276,6 +276,7 @@
 					break;
 				}
 				up->arg = argv;
+				argv[argc] = nil;
 				break; /* for binary */
 			}
 		}
--- a/librc/drawcpu.c
+++ b/librc/drawcpu.c
@@ -30,7 +30,6 @@
 	0
 };
 
-/* TODO: Set rcmain in .make  */
 char Rcmain[]="/usr/local/lib/rcmain";
 char Fdprefix[]="/fd/";
 
--- a/librc/exec.c
+++ b/librc/exec.c
@@ -252,6 +252,7 @@
 	setvar("cflag", flag['c']?newword(flag['c'][0], (word *)0)
 				:(word *)0);
 	setvar("rcname", newword(argv[0], (word *)0));
+	// TODO: Move the rcmain to a straight up bootstrap instead
 	bootstrap[0].i = 1;
 	bootstrap[1].s="*bootstrap*";
 	bootstrap[2].f = Xmark;
--- a/main.c
+++ b/main.c
@@ -54,8 +54,10 @@
 {
 	extern ulong kerndate;
 	int ifd, ofd,efd, dfd;
+	char *path;
 
 	debug = 0;
+	path = nil;
 	kerndate = seconds();
 	eve = getuser();
 	if(eve == nil)
@@ -65,6 +67,9 @@
 	case 'D':
 		debug++;
 		break;
+	case 'p':
+		path = EARGF(usage());
+		break;
 	default:
 		usage();
 	} ARGEND;
@@ -105,6 +110,9 @@
 
 	if(bind("/root", "/", MAFTER) < 0)
 		panic("bind /root: %r");
+
+	if(path != nil)
+		bind(path, "/bin", MAFTER);
 
 	char *cmd[] = {
 		"drawcpu",
--- a/posix-386/Makefile
+++ b/posix-386/Makefile
@@ -7,7 +7,6 @@
 OFILES=\
 	getcallerpc.$O\
 	tas.$O\
-	start.$O\
 
 default: $(LIB)
 $(LIB): $(OFILES)
--- a/posix-386/start.c
+++ /dev/null
@@ -1,38 +1,0 @@
-#include "u.h"
-#include "libc.h"
-
-void start(uintptr_t entry, Tos *_tos, int argc, char *argv[]) {
-    // entry point
-    register uintptr_t ebx asm("ebx") = entry;
-    register Tos *ecx asm("ecx") = _tos;
-    register int edx asm("edx") = argc;
-    register char **esi asm("esi") = argv;
-
-    __asm__ (
-        // Load values into registers
-        "mov ebx, %0\n\t"
-        "mov ecx, %1\n\t"
-        "mov edx, %2\n\t"
-        "mov esi, %3\n\t"
-
-        // push argv onto stack
-        "mov edi, edx\n\t"
-        "inc edi\n\t"
-        "shl edi, 2\n\t"
-        "sub esp, edi\n\t"
-        "mov edi, esp\n\t"
-        "rep movsb\n\t"
-
-        // push argc onto stack
-        "mov [esp], edx\n\t"
-        "sub esp, 4\n\t"
-
-        // jump to entry point
-        "jmp ebx\n\t"
-        "nop\n\t"
-
-        :
-        : "r" (ebx), "r" (ecx), "r" (edx), "r" (esi)
-        : "edi"
-    );
-}
--- a/posix-amd64/Makefile
+++ b/posix-amd64/Makefile
@@ -7,7 +7,6 @@
 OFILES=\
 	getcallerpc.$O\
 	tas.$O\
-	start.$O\
 	trampoline.$O\
 
 default: $(LIB)
--- a/posix-amd64/start.s
+++ /dev/null
@@ -1,22 +1,0 @@
-.text
-.globl start
-start:
-	mov %rdi, %rbp /* entry */
-	mov %rsi, %rax /* _tos */
-	mov %rdx, %rbx /* argc */
-	mov %rcx, %rsi /* argv */
-
-	/* push argv onto stack */
-	mov %rbx, %rcx
-	add $1, %rcx
-	sal $3, %rcx
-	sub %rcx, %rsp
-	mov %rsp, %rdi
-	rep movsb
-
-	/* push argc onto stack */
-	push %rbx
-
-	jmp *%rbp
-
-.section .note.GNU-stack,"",@progbits
\ No newline at end of file
--- a/posix-arm/Makefile
+++ b/posix-arm/Makefile
@@ -7,7 +7,6 @@
 OFILES=\
 	getcallerpc.$O\
 	tas.$O\
-	start.$O\
 
 default: $(LIB)
 $(LIB): $(OFILES)
--- a/posix-arm/start.c
+++ /dev/null
@@ -1,70 +1,0 @@
-#include "u.h"
-#include "libc.h"
-
-void start(uintptr_t entry, Tos *_tos, int argc, char *argv[]) {
-    // entry point
-    register uintptr_t r0 asm("r0") = entry;
-    register Tos *r1 asm("r1") = _tos;
-    register int r2 asm("r2") = argc;
-    register char **r3 asm("r3") = argv;
-
-    __asm__ __volatile__ (
-        // Load values into registers
-        "mov r0, %0\n\t"
-        "mov r1, %1\n\t"
-        "mov r2, %2\n\t"
-        "mov r3, %3\n\t"
-
-    #ifdef ARMV5
-        // ARMv5 specific code
-        // push argv onto stack
-        "mov r4, r2\n\t"
-        "add r4, r4, #1\n\t"
-        "lsl r4, r4, #2\n\t"
-        "sub sp, sp, r4\n\t"
-        "mov r5, sp\n\t"
-        "cpy r6, r3\n\t"
-
-
-    "copy_argv_loop:\n\t"
-        "ldr r7, [r6], #4\n\t"
-        "str r7, [r5], #4\n\t"
-        "subs r4, r4, #1\n\t"
-        "bne copy_argv_loop\n\t"
-
-        // push argc onto stack
-        "sub sp, sp, #4\n\t"
-        "str r2, [sp]\n\t"
-
-    #else
-        // Generic ARM code
-        // push argv onto stack
-        "ldr r4, [r3]\n\t"
-        "add r4, r4, #1\n\t"
-        "lsl r4, r4, #2\n\t"
-        "sub sp, sp, r4\n\t"
-        "mov r5, sp\n\t"
-        "cpy r6, r3\n\t"
-
-    "copy_argv_loop:\n\t"
-        "ldr r7, [r6], #4\n\t"
-        "str r7, [r5], #4\n\t"
-        "subs r4, r4, #1\n\t"
-        "bne copy_argv_loop\n\t"
-
-        // push argc onto stack
-        "sub sp, sp, #4\n\t"
-        "str r2, [sp]\n\t"
-
-    #endif
-
-        // jump to entry point
-        "mov lr, r0\n\t"
-        "bx lr\n\t"
-        "nop\n\t"
-
-        :
-        : "r" (r0), "r" (r1), "r" (r2), "r" (r3)
-        : "r4", "r5", "r6", "r7"
-    );
-}
\ No newline at end of file
--- a/posix-arm64/Makefile
+++ b/posix-arm64/Makefile
@@ -7,7 +7,6 @@
 OFILES=\
 	getcallerpc.$O\
 	tas.$O\
-	start.$O\
 	trampoline.$O\
 	patch.$O\
 
--- a/posix-arm64/patch.c
+++ b/posix-arm64/patch.c
@@ -11,11 +11,11 @@
         // MOV X0, #immediate (could be D2800000 | syscall_number)
         // BL X0 (could be D4000000 | offset)
         // 0xD63F0000 is our BLR X0
-        // This sets up our jump to the trampoline code
+        // This sets up our jmp to the trampoline code
         if ((*(ulong*)&text[i] & 0xFFFF0000) == 0xD2800000 && (*(ulong*)&text[i+BY2SE] & 0xFFFF0000) == 0xD4000000) {
             ulong *ptr = (ulong*)&text[i];
-            ptr[1] = 0x3F;
             ptr[0] = 0xD6;
+            ptr[1] = 0x3F;
             ret++;
         }
     }
--- a/posix-arm64/start.s
+++ /dev/null
@@ -1,48 +1,0 @@
-.text
-.global _start
-.align 4
-_start:
-    // Function prologue
-    stp x29, x30, [sp, #-16]!  // Save frame pointer and link register
-    mov x29, sp                // Set up frame pointer
-
-    // Save callee-saved registers
-    stp x19, x20, [sp, #-16]!
-
-    // Your original code starts here
-    mov x19, x0         // entry (x0 is the first argument)
-    mov x9, x1          // _tos (x1 is the second argument)
-    mov x20, x2         // argc (x2 is the third argument)
-    mov x21, x3         // argv (x3 is the fourth argument)
-
-    // Push argv onto stack
-    mov x10, x20        // x10 = argc
-    add x10, x10, #1    // x10 = argc + 1
-    lsl x10, x10, #3    // x10 = (argc + 1) * 8
-    sub sp, sp, x10     // Allocate space on stack
-    
-    // Ensure 16-byte stack alignment
-    and x11, x10, #15   // Get the misalignment amount
-    sub sp, sp, x11     // Adjust stack to ensure alignment
-
-    mov x0, sp          // x0 = new stack pointer (destination for memcpy)
-    mov x1, x21         // x1 = argv (source for memcpy)
-    mov x2, x10         // x2 = number of bytes to copy
-    bl _memcpy
-
-    // Push argc onto stack (after the argv data)
-    str x20, [sp, x10]
-
-    // Call the entry point
-    blr x19
-
-    // Restore stack pointer
-    mov sp, x29
-
-    // Restore callee-saved registers
-    ldp x19, x20, [sp], #16
-
-    // Function epilogue
-    ldp x29, x30, [sp], #16
-
-    ret
\ No newline at end of file
--- a/posix-arm64/trampoline.c
+++ b/posix-arm64/trampoline.c
@@ -22,7 +22,7 @@
     	"mov x5, x4 \n\t"                 // x4 -> x5
     	"mov x6, x5 \n\t"                 // x5 -> x6
     	"ldr x7, [sp, #24] \n\t"          // Load original x8 into x7
-    	"bl _sysintercept \n\t"            // Call syscall function
+    	"bl _sysintercept \n\t"           // Call syscall function
     	"mov sp, x29 \n\t"                // Restore stack pointer
     	"ldp x29, x30, [sp], #16 \n\t"    // Restore frame pointer and link register
     	"ret \n\t"                        // Return
@@ -38,7 +38,7 @@
         ((uint8_t *)text)[i] = 0x90;
 
     /* Preserve redzone */
-    ((uint8_t*)text)[_NSYS + 0x00] = 0x48;
+    ((uint8_t *) text)[_NSYS + 0x00] = 0x48;
     ((uint8_t *) text)[_NSYS + 0x01] = 0x81;
 	((uint8_t *) text)[_NSYS + 0x02] = 0xec;
 	((uint8_t *) text)[_NSYS + 0x03] = 0x80;
--- a/posix-mips/Makefile
+++ b/posix-mips/Makefile
@@ -7,7 +7,6 @@
 OFILES=\
 	getcallerpc.$O\
 	tas.$O\
-	start.$O\
 	
 default: $(LIB)
 $(LIB): $(OFILES)
--- a/posix-mips/start.s
+++ /dev/null
@@ -1,41 +1,0 @@
-.data
-argc:   .word 0
-_tos:   .word 0
-argv:   .word 0
-sp:     .word 0
-
-.text
-.globl start
-start:
-    # entry point
-    lw $t0, argc       # $t0 = argc
-    lw $t1, _tos       # $t1 = _tos
-    lw $t2, argv       # $t2 = argv
-    lw $t3, sp         # $t3 = stack pointer
-
-    # push argv onto stack
-    move $t4, $t0      # $t4 = argc
-    addi $t4, $t4, 1   # $t4 = argc + 1
-    sll $t4, $t4, 2    # $t4 = (argc + 1) * 4
-    subu $sp, $sp, $t4 # $sp = $sp - ((argc + 1) * 4)
-    move $t4, $sp      # $t4 = stack pointer
-    move $t5, $t2      # $t5 = argv
-    move $t6, $t4      # $t6 = destination pointer
-
-copy_argv_loop:
-    lw $t7, 0($t5)     # Load argv[i] to $t7
-    sw $t7, 0($t6)     # Store argv[i] to stack
-    addiu $t5, $t5, 4  # Increment argv
-    addiu $t6, $t6, 4  # Increment destination pointer
-    addi $t0, $t0, -1  # Decrement argc
-    bne $t0, $zero, copy_argv_loop # Loop if argc != 0
-
-    # push argc onto stack
-    subu $sp, $sp, 4   # $sp = $sp - 4
-    sw $t3, ($sp)      # Push stack pointer
-
-    # jump to entry point
-    j $t0
-    nop
-
-.section .note.GNU-stack,"",@progbits
\ No newline at end of file
--- a/posix-power/Makefile
+++ b/posix-power/Makefile
@@ -9,7 +9,6 @@
 OFILES=\
 	getcallerpc.$O\
 	tas.$O\
-	start.$O\
 
 default: $(LIB)
 $(LIB): $(OFILES)
--- a/posix-power/start.c
+++ /dev/null
@@ -1,49 +1,0 @@
-#include "u.h"
-#include "libc.h"
-
-void start() {
-    // entry point
-    register int argc asm("r3");
-    register int _tos asm("r4");
-    register int argv asm("r5");
-    register int sp asm("r6");
-
-    __asm__ (
-        // Load values into registers
-        "lwz %0, argc\n\t"
-        "lwz %1, _tos\n\t"
-        "lwz %2, argv\n\t"
-        "lwz %3, sp\n\t"
-
-        // push argv onto stack
-        "mr r4, r3\n\t"
-        "addi r4, r4, 1\n\t"
-        "slwi r4, r4, 2\n\t"
-        "subi sp, sp, r4\n\t"
-        "mr r4, sp\n\t"
-        "mr r5, r5\n\t"
-        "mr r6, r4\n\t"
-
-    "copy_argv_loop:\n\t"
-        "lwz r7, 0(r5)\n\t"
-        "stw r7, 0(r6)\n\t"
-        "addi r5, r5, 4\n\t"
-        "addi r6, r6, 4\n\t"
-        "addi r3, r3, -1\n\t"
-        "cmpwi r3, 0\n\t"
-        "bne copy_argv_loop\n\t"
-
-        // push argc onto stack
-        "subi sp, sp, 4\n\t"
-        "stw r4, 0(sp)\n\t"
-
-        // jump to entry point
-        "mtctr r3\n\t"
-        "bctr\n\t"
-        "nop\n\t"
-
-        :
-        : "r" (argc), "r" (_tos), "r" (argv), "r" (sp)
-        : "r4", "r5", "r6", "r7"
-    );
-}
\ No newline at end of file
--- a/posix-riscv64/Makefile
+++ b/posix-riscv64/Makefile
@@ -7,7 +7,6 @@
 OFILES=\
 	getcallerpc.$O\
 	tas.$O\
-	start.$O\
 
 default: $(LIB)
 $(LIB): $(OFILES)
--- a/posix-riscv64/start.c
+++ /dev/null
@@ -1,47 +1,0 @@
-#include "u.h"
-#include "libc.h"
-
-void start() {
-    // entry point
-    register int argc asm("t0");
-    register int _tos asm("t1");
-    register int argv asm("t2");
-    register int sp asm("t3");
-
-    __asm__ (
-        // Load values into registers
-        "lw %0, argc\n\t"
-        "lw %1, _tos\n\t"
-        "lw %2, argv\n\t"
-        "lw %3, sp\n\t"
-
-        // push argv onto stack
-        "mv t4, t0\n\t"
-        "addi t4, t4, 1\n\t"
-        "slli t4, t4, 2\n\t"
-        "sub sp, sp, t4\n\t"
-        "mv t4, sp\n\t"
-        "mv t5, t2\n\t"
-        "mv t6, t4\n\t"
-
-    "copy_argv_loop:\n\t"
-        "lw t7, 0(t5)\n\t"
-        "sw t7, 0(t6)\n\t"
-        "addi t5, t5, 4\n\t"
-        "addi t6, t6, 4\n\t"
-        "addi t0, t0, -1\n\t"
-        "bnez t0, copy_argv_loop\n\t"
-
-        // push argc onto stack
-        "addi sp, sp, -4\n\t"
-        "sw t3, 0(sp)\n\t"
-
-        // jump to entry point
-        "jr t0\n\t"
-        "nop\n\t"
-
-        :
-        : "r" (argc), "r" (_tos), "r" (argv), "r" (sp)
-        : "t4", "t5", "t6", "t7"
-    );
-}
\ No newline at end of file