From 998b3ac86e3b3f926d81e768f1200815e9e3d148 Mon Sep 17 00:00:00 2001 From: tcsullivan Date: Thu, 18 Jul 2019 18:43:22 -0400 Subject: [PATCH] Fixed syscalls, initrd program works --- run.sh | 2 +- src/initrd/Makefile | 1 + src/initrd/init.c | 3 +- src/kernel/clock.c | 14 +-- src/kernel/gpio.c | 20 +-- src/kernel/svc.c | 25 ++-- src/kernel/task.c | 22 ++-- src/kernel/vfs.c | 16 +-- src/libgpio/gpio.c | 72 +++++------ src/libgpio/libgpio.a | Bin 5772 -> 5808 bytes .../platform/stmos/functions/os/syscalls.c | 118 ++++++++---------- src/pdclib/stmos_install_headers.sh | 6 + 12 files changed, 151 insertions(+), 148 deletions(-) create mode 100755 src/pdclib/stmos_install_headers.sh diff --git a/run.sh b/run.sh index a1c344c..8253d05 100755 --- a/run.sh +++ b/run.sh @@ -3,7 +3,7 @@ # Starts openocd and connects gdb to the target, for programming/debugging # -openocd -f /usr/share/openocd/scripts/board/st_nucleo_l476rg.cfg & +openocd -f /usr/local/share/openocd/scripts/board/st_nucleo_l4.cfg & sleep 1 # Need to wait some time for openocd to connect gdb-multiarch -ex "target remote localhost:3333" main.elf pkill openocd # Ensure openocd exits when we're done diff --git a/src/initrd/Makefile b/src/initrd/Makefile index 82785f4..3385008 100644 --- a/src/initrd/Makefile +++ b/src/initrd/Makefile @@ -16,6 +16,7 @@ all: $(OFILES) files/%: %.c @echo " CC " $< @$(CROSS)$(CC) $(CFLAGS) $< ../libgpio/libgpio.a -o $@ + @cp -v $@ init.debug @$(CROSS)strip $@ clean: diff --git a/src/initrd/init.c b/src/initrd/init.c index dc88ff8..ad3be41 100644 --- a/src/initrd/init.c +++ b/src/initrd/init.c @@ -10,8 +10,7 @@ int main(void) { gpioMode(5, OUTPUT); - - printf("Hello, world!\n"); + printf("Hello, world!\n\r"); while (1) { gpioWrite(5, 1); diff --git a/src/kernel/clock.c b/src/kernel/clock.c index a49b4c1..41a3199 100644 --- a/src/kernel/clock.c +++ b/src/kernel/clock.c @@ -27,14 +27,14 @@ volatile uint32_t clock_ticks = 0; volatile uint8_t tim2_finished = 1; -void clock_svc(uint32_t *args) +void clock_svc(uint32_t n, uint32_t *ret, uint32_t *args) { - if (args[0] == 0) - task_sleep(args[1]); - else if (args[0] == 1) - clock_udelay(args[1]); - else if (args[0] == 2) - *((unsigned int *)args[1]) = clock_millis(); + if (n == 0) + task_sleep(args[0]); + else if (n == 1) + clock_udelay(args[0]); + else if (n == 2) + *((unsigned int *)ret) = clock_millis(); } void clock_init(void) diff --git a/src/kernel/gpio.c b/src/kernel/gpio.c index 33423b9..3bd67cb 100644 --- a/src/kernel/gpio.c +++ b/src/kernel/gpio.c @@ -25,29 +25,29 @@ static GPIO_TypeDef *gpio_ports[8] = { GPIOE, GPIOF, GPIOG, GPIOH }; -void gpio_svc(uint32_t *args) +void gpio_svc(uint32_t n, uint32_t *ret, uint32_t *args) { - GPIO_TypeDef *port = gpio_ports[args[1] >> 4]; - uint32_t pin = args[1] & 0xF; + GPIO_TypeDef *port = gpio_ports[args[0] >> 4]; + uint32_t pin = args[0] & 0xF; - switch (args[0]) { + switch (n) { case 0: - gpio_mode(port, pin, args[2]); + gpio_mode(port, pin, args[1]); break; case 1: - gpio_type(port, pin, args[2]); + gpio_type(port, pin, args[1]); break; case 2: - gpio_pupd(port, pin, args[2]); + gpio_pupd(port, pin, args[1]); break; case 3: - gpio_speed(port, pin, args[2]); + gpio_speed(port, pin, args[1]); break; case 4: - gpio_dout(port, pin, args[2]); + gpio_dout(port, pin, args[1]); break; case 5: - *((int *)args[2]) = gpio_din(port, pin); + *((int *)ret) = gpio_din(port, pin); break; } } diff --git a/src/kernel/svc.c b/src/kernel/svc.c index 9044141..7c81e5f 100644 --- a/src/kernel/svc.c +++ b/src/kernel/svc.c @@ -23,22 +23,25 @@ #include "clock.h" #include "task.h" -extern void gpio_svc(uint32_t *); -extern void clock_svc(uint32_t *); -extern void task_svc(uint32_t *); -extern void vfs_svc(uint32_t *args); +extern void gpio_svc(uint32_t, uint32_t *, uint32_t *); +extern void clock_svc(uint32_t, uint32_t *, uint32_t *); +extern void task_svc(uint32_t, uint32_t *, uint32_t *); +extern void vfs_svc(uint32_t, uint32_t *, uint32_t *args); void SVC_Handler(void) { - uint32_t *args; + uint32_t *stack; asm("\ tst lr, #4; \ ite eq; \ mrseq %0, msp; \ mrsne %0, psp; \ - " : "=r" (args)); + " : "=r" (stack)); - int svc_number = ((char *)args[6])[-2]; + int svc_number = ((char *)stack[6])[-2]; + uint32_t min_number = stack[0]; + uint32_t *ret = (uint32_t *)stack[1]; + uint32_t *args = (uint32_t *)stack[2]; switch (svc_number) { case -1: @@ -50,7 +53,7 @@ void SVC_Handler(void) { * 4 - sbrk (TODO bad) * 5 - execve */ - task_svc(args); + task_svc(min_number, ret, args); break; case 1: /* GPIO-related calls @@ -61,7 +64,7 @@ void SVC_Handler(void) { * 4 - gpio_dout * 5 - gpio_din */ - gpio_svc(args); + gpio_svc(min_number, ret, args); break; case 2: /* Clock-related calls @@ -69,7 +72,7 @@ void SVC_Handler(void) { * 1 - udelay * 2 - ticks */ - clock_svc(args); + clock_svc(min_number, ret, args); break; case 3: /* Filesystem-related calls @@ -79,7 +82,7 @@ void SVC_Handler(void) { * 3 - read * 4 - write */ - vfs_svc(args); + vfs_svc(min_number, ret, args); break; default: break; diff --git a/src/kernel/task.c b/src/kernel/task.c index c5d09d4..304e608 100644 --- a/src/kernel/task.c +++ b/src/kernel/task.c @@ -31,28 +31,28 @@ static task_t *task_queue; static uint8_t task_disable = 0; static pid_t task_next_pid = 0; -void task_svc(uint32_t *args) +void task_svc(uint32_t n, uint32_t *ret, uint32_t *args) { - switch (args[0]) { + switch (n) { case 0: - task_exit(args[1]); + task_exit(args[0]); break; case 1: - *((int *)args[1]) = task_fork(); + *((int *)ret) = task_fork(); break; case 2: - *((int *)args[1]) = task_getpid(); + *((int *)ret) = task_getpid(); break; case 3: - *((int *)args[4]) = task_waitpid(args[1], (int *)args[2], - args[3]); + *((int *)ret) = task_waitpid(args[0], (int *)args[1], + args[2]); break; case 4: - *((void **)args[2]) = task_sbrk(args[1]); + *((void **)ret) = task_sbrk(args[0]); break; case 5: - *((uint32_t *)args[4]) = elf_execve((const char *)args[1], - (char * const *)args[2], (char * const *)args[3]); + *((uint32_t *)ret) = elf_execve((const char *)args[0], + (char * const *)args[1], (char * const *)args[2]); break; default: break; @@ -325,7 +325,7 @@ void PendSV_Handler(void) task_current = task_queue; if (task_current->status.state == TASK_SLEEPING && - task_current->status.value <= ticks) + task_current->status.value < ticks) task_current->status.state = TASK_RUNNING; } while (task_current->status.state != TASK_RUNNING); diff --git a/src/kernel/vfs.c b/src/kernel/vfs.c index b9be9bc..8d4b975 100644 --- a/src/kernel/vfs.c +++ b/src/kernel/vfs.c @@ -35,24 +35,24 @@ static vfs_file vfs_files[VFS_MAX_FILES]; if (pid != fpid && fpid != 0) \ return 0; } -void vfs_svc(uint32_t *args) +void vfs_svc(uint32_t n, uint32_t *ret, uint32_t *args) { - switch (args[0]) { + switch (n) { case 0: - *((int *)args[3]) = vfs_mount((vfs_volume_funcs *)args[1], - args[2]); + *((int *)ret) = vfs_mount((vfs_volume_funcs *)args[0], + args[1]); break; case 1: - *((int *)args[3]) = vfs_open((const char *)args[1], args[2]); + *((int *)ret) = vfs_open((const char *)args[0], args[1]); break; case 2: - *((int *)args[2]) = vfs_close(args[1]); + *((int *)ret) = vfs_close(args[0]); break; case 3: - *((int *)args[4]) = vfs_read(args[1], args[2], (uint8_t *)args[3]); + *((int *)ret) = vfs_read(args[0], args[1], (uint8_t *)args[2]); break; case 4: - *((int *)args[4]) = vfs_write(args[1], args[2], (const uint8_t *)args[3]); + *((int *)ret) = vfs_write(args[0], args[1], (const uint8_t *)args[2]); break; default: break; diff --git a/src/libgpio/gpio.c b/src/libgpio/gpio.c index fdc877c..522817f 100644 --- a/src/libgpio/gpio.c +++ b/src/libgpio/gpio.c @@ -2,77 +2,79 @@ #include +#define NOOPTIMIZE __attribute__((optimize(0))) + +NOOPTIMIZE void gpioMode(gpio_pin_t pin, int mode) { - register uint32_t r1 __asm("r1") = pin; - register uint32_t r2 __asm("r2") = mode; - __asm("\ + uint32_t args[2] = { (uint32_t)pin, mode }; + __asm volatile("\ mov r0, 0; \ - mov r1, %0; \ - mov r2, %1; \ + mov r1, 0; \ + mov r2, %0; \ svc 1; \ - " :: "r" (r1), "r" (r2)); + " :: "r" (args)); } +NOOPTIMIZE void gpioType(gpio_pin_t pin, int type) { - register uint32_t r1 __asm("r1") = pin; - register uint32_t r2 __asm("r2") = type; - __asm("\ + uint32_t args[2] = { (uint32_t)pin, type }; + __asm volatile("\ mov r0, 1; \ - mov r1, %0; \ - mov r2, %1; \ + mov r1, 0; \ + mov r2, %0; \ svc 1; \ - " :: "r" (r1), "r" (r2)); + " :: "r" (args)); } +NOOPTIMIZE void gpioPuPd(gpio_pin_t pin, int pupd) { - register uint32_t r1 __asm("r1") = pin; - register uint32_t r2 __asm("r2") = pupd; - __asm("\ + uint32_t args[2] = { (uint32_t)pin, pupd }; + __asm volatile("\ mov r0, 2; \ - mov r1, %0; \ - mov r2, %1; \ + mov r1, 0; \ + mov r2, %0; \ svc 1; \ - " :: "r" (r1), "r" (r2)); + " :: "r" (args)); } +NOOPTIMIZE void gpioSpeed(gpio_pin_t pin, int speed) { - register uint32_t r1 __asm("r1") = pin; - register uint32_t r2 __asm("r2") = speed; - __asm("\ + uint32_t args[2] = { (uint32_t)pin, speed }; + __asm volatile("\ mov r0, 3; \ - mov r1, %0; \ - mov r2, %1; \ + mov r1, 0; \ + mov r2, %0; \ svc 1; \ - " :: "r" (r1), "r" (r2)); + " :: "r" (args)); } +NOOPTIMIZE void gpioWrite(gpio_pin_t pin, int value) { - register uint32_t r1 __asm("r1") = pin; - register uint32_t r2 __asm("r2") = value; - __asm("\ + uint32_t args[2] = { (uint32_t)pin, value }; + __asm volatile("\ mov r0, 4; \ - mov r1, %0; \ - mov r2, %1; \ + mov r1, 0; \ + mov r2, %0; \ svc 1; \ - " :: "r" (r1), "r" (r2)); + " :: "r" (args)); } +NOOPTIMIZE int gpioRead(gpio_pin_t pin) { - int ret = 0; - register uint32_t r1 __asm("r1") = pin; - register uint32_t r2 __asm("r2") = (uint32_t)&ret; - __asm("\ + volatile int ret = 0; + uint32_t args[1] = { (uint32_t)pin }; + __asm volatile("\ mov r0, 5; \ mov r1, %0; \ mov r2, %1; \ svc 1; \ - " :: "r" (r1), "r" (r2)); + " :: "r" (&ret), "r" (args)); return ret; } diff --git a/src/libgpio/libgpio.a b/src/libgpio/libgpio.a index b8c781de136b452a0ea69d15200c57c8cf03e195..36cb0e8672a51d0453e4013bcd19c797899547b0 100644 GIT binary patch literal 5808 zcmbVQd2C!q8J~G?FYi9DkF^~;2doq4sEBv%#EG5Tu2YAQqo#+Nh`{=J_jz~eUfLrh z4T7K~5TH^>fEJ+?wNN0H5D5@O1x4f!q?TKVfBHut6#}Hv3MB|cX(Gz+o8$Ry+K_~i z=Kbb7<~QGb^LA$5_HVYU>D<Ug{xf05SOE1) z?^Lsue0fhf<1lJGbhsiL(~aqj&^%ai9NBuLny*Xa1CA|KiSm3@-gt(Gh7gSVTt9+X z)~(yHUPd$%{`h^nr#R;bM;6Xjv3ZHHKXnSde?-747Gs_4=*1Hk*!e><8)lB=el)Ww zcYP*!Eo9z*l`&MlaEf1HKj|9%@zl{5Dm!lF%kRS1xC>wNE_{9e!znGt+^*+(r}XT6e#U$vGd{l2WXw8=+$~Z03oA5u=!C(T`4}F?P|q0r4PWpG ztUjMVU#c_zHlgzWks##%1_2LTBryEXQP+V#^^I*2k!uPf8|>jAH_E zGiZd`(U3#57$Qn$C5a|1C7*SLXd+SyS$UEyDOuJPN<*uZ+O4NZib|==`W{JbQtGw# z((vuZN*Zo4CcO_l;op)nW-O)PBcFrNWp*2(y>O8cEs(vL^*iF~H7qjuSm+CI{ScZ_ zT2+&UJIK7qSiTGxN#JqOTw;U*n1yoOg`(w)U97iBNpY_TxwW7uw|?R zWLqYsq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