From fd13e7f08cb7755357b5d27f45d5bad905568f39 Mon Sep 17 00:00:00 2001 From: Alan Cox Date: Sun, 23 Nov 2014 22:36:10 +0000 Subject: [PATCH] execve: per CPU magic, also allow for different program base Use different magic numbers for different CPU types. Note that we need to pass some kind of base info and check it in future (or support relocatable binaries ?) As noted by Atsidaev our handling of some of this was rather confused. We now pass the size (top - PROGBASE) to the allocator. This sort of assumes for 6502 we'll set 0x0200 (or higher) entry and 0x0000 base and something similar for 6809. zx128 will still need a custom banking module eventually. Once someone beats SDCC into banking the kernel code that can mostly live in 2 (or 3) 16K banks in an interface 2 cartridge with banking support then it can use 0x8000-0xBFFF and lazy memcpy it from another page (or even exchange two pages) on a taskswitch between two different 32K apps. See: http://www.fruitcake.plus.com/Sinclair/Interface2/Cartridges/Interface2_RC_Custom.htm#SoftwarePaging --- Kernel/cpu-6502/cpu.h | 2 ++ Kernel/cpu-6809/cpu.h | 1 + Kernel/cpu-z80/cpu.h | 3 +++ Kernel/include/kernel.h | 1 - Kernel/syscall_exec.c | 19 +++++++++++-------- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Kernel/cpu-6502/cpu.h b/Kernel/cpu-6502/cpu.h index 208cb2af..f2cf5069 100644 --- a/Kernel/cpu-6502/cpu.h +++ b/Kernel/cpu-6502/cpu.h @@ -20,6 +20,8 @@ extern void *memcpy(void *, void *, size_t); extern void *memset(void *, int, size_t); extern size_t strlen(const char *); +#define EMAGIC 0x4C /* Header of executable (JMP) */ + #define staticfast static /* FIXME: need to add 64bit helper/struct magic for this compiler */ diff --git a/Kernel/cpu-6809/cpu.h b/Kernel/cpu-6809/cpu.h index 13489f1e..575896f5 100644 --- a/Kernel/cpu-6809/cpu.h +++ b/Kernel/cpu-6809/cpu.h @@ -16,6 +16,7 @@ extern void ei(void); extern irqflags_t di(void); extern void irqrestore(irqflags_t f); +#define EMAGIC 0x0E /* Header of executable (JMP) */ extern void *memcpy(void *, void *, size_t); extern void *memset(void *, int, size_t); diff --git a/Kernel/cpu-z80/cpu.h b/Kernel/cpu-z80/cpu.h index 26697f90..e7774d59 100644 --- a/Kernel/cpu-z80/cpu.h +++ b/Kernel/cpu-z80/cpu.h @@ -16,6 +16,9 @@ typedef uint16_t irqflags_t; extern irqflags_t di(void); extern void irqrestore(irqflags_t f); +/* Z80 binaries start with a JP */ +#define EMAGIC 0xc3 /* Header of executable */ + /* compiler provides optimised versions of these: */ #if defined(__SDCC_z80) || defined(__SDCC_z180) || defined(__SDCC_r2k) || defined(__SDCC_r3ka) #define memcpy(dst, src, n) __builtin_memcpy(dst, src, n) diff --git a/Kernel/include/kernel.h b/Kernel/include/kernel.h index eadaee62..018923c6 100644 --- a/Kernel/include/kernel.h +++ b/Kernel/include/kernel.h @@ -54,7 +54,6 @@ From UZI by Doug Braun and UZI280 by Stefan Nitschke. #define NSIGS 32 /* Number of signals <= 32 */ #define ROOTINODE 1 /* Inode # of / for all mounted filesystems. */ -#define EMAGIC 0xc3 /* Header of executable */ #define CMAGIC 24721 /* Random number for cinode c_magic */ #define SMOUNTED 12742 /* Magic number to specify mounted filesystem */ diff --git a/Kernel/syscall_exec.c b/Kernel/syscall_exec.c index 8e3bf36c..2fb54e50 100644 --- a/Kernel/syscall_exec.c +++ b/Kernel/syscall_exec.c @@ -58,12 +58,15 @@ int16_t _execve(void) /* Read in the first block of the new program */ buf = bread(ino->c_dev, bmap(ino, 0, 1), 0); - /**************************************** - * Get magic number into var magic - * C3 : executable file no C/D sep. - * 00FF : " " with C/D sep. (not supported in FUZIX) - * other : maybe shell script (nogood2) - ****************************************/ + /* Magic numbers + 0xC3 xx xx - Z80 with 0x100 entry + 0x4C xx xx - 6502 + 0x0E xx xx - 6809 + + others TBD + + FIXME: need to modify header design to include base addr page + */ if ((*buf & 0xff) != EMAGIC) { udata.u_error = ENOEXEC; goto nogood2; @@ -99,7 +102,7 @@ int16_t _execve(void) } /* Binary doesn't fit */ - if (top < ino->c_node.i_size + 1024) { + if (top - PROGBASE < ino->c_node.i_size + 1024) { udata.u_error = ENOMEM; goto nogood2; } @@ -114,7 +117,7 @@ int16_t _execve(void) goto nogood3; /* SN */ /* This must be the last test as it makes changes if it works */ - if (pagemap_realloc(top)) + if (pagemap_realloc(top - PROGBASE)) goto nogood3; /* From this point on we are commmited to the exec() completing */ -- 2.34.1