Fixed syscalls, initrd program works

master
tcsullivan 5 years ago
parent b92ccff9b1
commit 998b3ac86e

@ -3,7 +3,7 @@
# Starts openocd and connects gdb to the target, for programming/debugging # 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 sleep 1 # Need to wait some time for openocd to connect
gdb-multiarch -ex "target remote localhost:3333" main.elf gdb-multiarch -ex "target remote localhost:3333" main.elf
pkill openocd # Ensure openocd exits when we're done pkill openocd # Ensure openocd exits when we're done

@ -16,6 +16,7 @@ all: $(OFILES)
files/%: %.c files/%: %.c
@echo " CC " $< @echo " CC " $<
@$(CROSS)$(CC) $(CFLAGS) $< ../libgpio/libgpio.a -o $@ @$(CROSS)$(CC) $(CFLAGS) $< ../libgpio/libgpio.a -o $@
@cp -v $@ init.debug
@$(CROSS)strip $@ @$(CROSS)strip $@
clean: clean:

@ -10,8 +10,7 @@
int main(void) int main(void)
{ {
gpioMode(5, OUTPUT); gpioMode(5, OUTPUT);
printf("Hello, world!\n\r");
printf("Hello, world!\n");
while (1) { while (1) {
gpioWrite(5, 1); gpioWrite(5, 1);

@ -27,14 +27,14 @@ volatile uint32_t clock_ticks = 0;
volatile uint8_t tim2_finished = 1; 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) if (n == 0)
task_sleep(args[1]); task_sleep(args[0]);
else if (args[0] == 1) else if (n == 1)
clock_udelay(args[1]); clock_udelay(args[0]);
else if (args[0] == 2) else if (n == 2)
*((unsigned int *)args[1]) = clock_millis(); *((unsigned int *)ret) = clock_millis();
} }
void clock_init(void) void clock_init(void)

@ -25,29 +25,29 @@ static GPIO_TypeDef *gpio_ports[8] = {
GPIOE, GPIOF, GPIOG, GPIOH 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]; GPIO_TypeDef *port = gpio_ports[args[0] >> 4];
uint32_t pin = args[1] & 0xF; uint32_t pin = args[0] & 0xF;
switch (args[0]) { switch (n) {
case 0: case 0:
gpio_mode(port, pin, args[2]); gpio_mode(port, pin, args[1]);
break; break;
case 1: case 1:
gpio_type(port, pin, args[2]); gpio_type(port, pin, args[1]);
break; break;
case 2: case 2:
gpio_pupd(port, pin, args[2]); gpio_pupd(port, pin, args[1]);
break; break;
case 3: case 3:
gpio_speed(port, pin, args[2]); gpio_speed(port, pin, args[1]);
break; break;
case 4: case 4:
gpio_dout(port, pin, args[2]); gpio_dout(port, pin, args[1]);
break; break;
case 5: case 5:
*((int *)args[2]) = gpio_din(port, pin); *((int *)ret) = gpio_din(port, pin);
break; break;
} }
} }

@ -23,22 +23,25 @@
#include "clock.h" #include "clock.h"
#include "task.h" #include "task.h"
extern void gpio_svc(uint32_t *); extern void gpio_svc(uint32_t, uint32_t *, uint32_t *);
extern void clock_svc(uint32_t *); extern void clock_svc(uint32_t, uint32_t *, uint32_t *);
extern void task_svc(uint32_t *); extern void task_svc(uint32_t, uint32_t *, uint32_t *);
extern void vfs_svc(uint32_t *args); extern void vfs_svc(uint32_t, uint32_t *, uint32_t *args);
void SVC_Handler(void) { void SVC_Handler(void) {
uint32_t *args; uint32_t *stack;
asm("\ asm("\
tst lr, #4; \ tst lr, #4; \
ite eq; \ ite eq; \
mrseq %0, msp; \ mrseq %0, msp; \
mrsne %0, psp; \ 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) { switch (svc_number) {
case -1: case -1:
@ -50,7 +53,7 @@ void SVC_Handler(void) {
* 4 - sbrk (TODO bad) * 4 - sbrk (TODO bad)
* 5 - execve * 5 - execve
*/ */
task_svc(args); task_svc(min_number, ret, args);
break; break;
case 1: /* GPIO-related calls case 1: /* GPIO-related calls
@ -61,7 +64,7 @@ void SVC_Handler(void) {
* 4 - gpio_dout * 4 - gpio_dout
* 5 - gpio_din * 5 - gpio_din
*/ */
gpio_svc(args); gpio_svc(min_number, ret, args);
break; break;
case 2: /* Clock-related calls case 2: /* Clock-related calls
@ -69,7 +72,7 @@ void SVC_Handler(void) {
* 1 - udelay * 1 - udelay
* 2 - ticks * 2 - ticks
*/ */
clock_svc(args); clock_svc(min_number, ret, args);
break; break;
case 3: /* Filesystem-related calls case 3: /* Filesystem-related calls
@ -79,7 +82,7 @@ void SVC_Handler(void) {
* 3 - read * 3 - read
* 4 - write * 4 - write
*/ */
vfs_svc(args); vfs_svc(min_number, ret, args);
break; break;
default: default:
break; break;

@ -31,28 +31,28 @@ static task_t *task_queue;
static uint8_t task_disable = 0; static uint8_t task_disable = 0;
static pid_t task_next_pid = 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: case 0:
task_exit(args[1]); task_exit(args[0]);
break; break;
case 1: case 1:
*((int *)args[1]) = task_fork(); *((int *)ret) = task_fork();
break; break;
case 2: case 2:
*((int *)args[1]) = task_getpid(); *((int *)ret) = task_getpid();
break; break;
case 3: case 3:
*((int *)args[4]) = task_waitpid(args[1], (int *)args[2], *((int *)ret) = task_waitpid(args[0], (int *)args[1],
args[3]); args[2]);
break; break;
case 4: case 4:
*((void **)args[2]) = task_sbrk(args[1]); *((void **)ret) = task_sbrk(args[0]);
break; break;
case 5: case 5:
*((uint32_t *)args[4]) = elf_execve((const char *)args[1], *((uint32_t *)ret) = elf_execve((const char *)args[0],
(char * const *)args[2], (char * const *)args[3]); (char * const *)args[1], (char * const *)args[2]);
break; break;
default: default:
break; break;
@ -325,7 +325,7 @@ void PendSV_Handler(void)
task_current = task_queue; task_current = task_queue;
if (task_current->status.state == TASK_SLEEPING && if (task_current->status.state == TASK_SLEEPING &&
task_current->status.value <= ticks) task_current->status.value < ticks)
task_current->status.state = TASK_RUNNING; task_current->status.state = TASK_RUNNING;
} while (task_current->status.state != TASK_RUNNING); } while (task_current->status.state != TASK_RUNNING);

@ -35,24 +35,24 @@ static vfs_file vfs_files[VFS_MAX_FILES];
if (pid != fpid && fpid != 0) \ if (pid != fpid && fpid != 0) \
return 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: case 0:
*((int *)args[3]) = vfs_mount((vfs_volume_funcs *)args[1], *((int *)ret) = vfs_mount((vfs_volume_funcs *)args[0],
args[2]); args[1]);
break; break;
case 1: case 1:
*((int *)args[3]) = vfs_open((const char *)args[1], args[2]); *((int *)ret) = vfs_open((const char *)args[0], args[1]);
break; break;
case 2: case 2:
*((int *)args[2]) = vfs_close(args[1]); *((int *)ret) = vfs_close(args[0]);
break; break;
case 3: 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; break;
case 4: 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; break;
default: default:
break; break;

@ -2,77 +2,79 @@
#include <stdint.h> #include <stdint.h>
#define NOOPTIMIZE __attribute__((optimize(0)))
NOOPTIMIZE
void gpioMode(gpio_pin_t pin, int mode) void gpioMode(gpio_pin_t pin, int mode)
{ {
register uint32_t r1 __asm("r1") = pin; uint32_t args[2] = { (uint32_t)pin, mode };
register uint32_t r2 __asm("r2") = mode; __asm volatile("\
__asm("\
mov r0, 0; \ mov r0, 0; \
mov r1, %0; \ mov r1, 0; \
mov r2, %1; \ mov r2, %0; \
svc 1; \ svc 1; \
" :: "r" (r1), "r" (r2)); " :: "r" (args));
} }
NOOPTIMIZE
void gpioType(gpio_pin_t pin, int type) void gpioType(gpio_pin_t pin, int type)
{ {
register uint32_t r1 __asm("r1") = pin; uint32_t args[2] = { (uint32_t)pin, type };
register uint32_t r2 __asm("r2") = type; __asm volatile("\
__asm("\
mov r0, 1; \ mov r0, 1; \
mov r1, %0; \ mov r1, 0; \
mov r2, %1; \ mov r2, %0; \
svc 1; \ svc 1; \
" :: "r" (r1), "r" (r2)); " :: "r" (args));
} }
NOOPTIMIZE
void gpioPuPd(gpio_pin_t pin, int pupd) void gpioPuPd(gpio_pin_t pin, int pupd)
{ {
register uint32_t r1 __asm("r1") = pin; uint32_t args[2] = { (uint32_t)pin, pupd };
register uint32_t r2 __asm("r2") = pupd; __asm volatile("\
__asm("\
mov r0, 2; \ mov r0, 2; \
mov r1, %0; \ mov r1, 0; \
mov r2, %1; \ mov r2, %0; \
svc 1; \ svc 1; \
" :: "r" (r1), "r" (r2)); " :: "r" (args));
} }
NOOPTIMIZE
void gpioSpeed(gpio_pin_t pin, int speed) void gpioSpeed(gpio_pin_t pin, int speed)
{ {
register uint32_t r1 __asm("r1") = pin; uint32_t args[2] = { (uint32_t)pin, speed };
register uint32_t r2 __asm("r2") = speed; __asm volatile("\
__asm("\
mov r0, 3; \ mov r0, 3; \
mov r1, %0; \ mov r1, 0; \
mov r2, %1; \ mov r2, %0; \
svc 1; \ svc 1; \
" :: "r" (r1), "r" (r2)); " :: "r" (args));
} }
NOOPTIMIZE
void gpioWrite(gpio_pin_t pin, int value) void gpioWrite(gpio_pin_t pin, int value)
{ {
register uint32_t r1 __asm("r1") = pin; uint32_t args[2] = { (uint32_t)pin, value };
register uint32_t r2 __asm("r2") = value; __asm volatile("\
__asm("\
mov r0, 4; \ mov r0, 4; \
mov r1, %0; \ mov r1, 0; \
mov r2, %1; \ mov r2, %0; \
svc 1; \ svc 1; \
" :: "r" (r1), "r" (r2)); " :: "r" (args));
} }
NOOPTIMIZE
int gpioRead(gpio_pin_t pin) int gpioRead(gpio_pin_t pin)
{ {
int ret = 0; volatile int ret = 0;
register uint32_t r1 __asm("r1") = pin; uint32_t args[1] = { (uint32_t)pin };
register uint32_t r2 __asm("r2") = (uint32_t)&ret; __asm volatile("\
__asm("\
mov r0, 5; \ mov r0, 5; \
mov r1, %0; \ mov r1, %0; \
mov r2, %1; \ mov r2, %1; \
svc 1; \ svc 1; \
" :: "r" (r1), "r" (r2)); " :: "r" (&ret), "r" (args));
return ret; return ret;
} }

Binary file not shown.

@ -1,72 +1,73 @@
#include "syscalls.h" #include "syscalls.h"
#define NOOPTIMIZE __attribute__((optimize(0)))
// //
// Task-related calls // Task-related calls
NOOPTIMIZE
void _exit(int code) void _exit(int code)
{ {
register uint32_t r1 __asm("r1") = code; uint32_t args[1] = { code };
__asm("\ __asm volatile("\
mov r0, 0; \ mov r0, 0; \
mov r1, %0; \ mov r1, 0; \
mov r2, %0; \
svc 0; \ svc 0; \
" :: "r" (r1)); " :: "r" (args));
} }
NOOPTIMIZE
int fork(void) int fork(void)
{ {
int ret = 0; volatile uint32_t ret = 0;
register uint32_t r1 __asm("r1") = (uint32_t)&ret; __asm volatile("\
__asm("\
mov r0, 1; \ mov r0, 1; \
mov r1, %0; \ mov r1, %0; \
mov r2, 0; \
svc 0; \ svc 0; \
" :: "r" (r1)); " :: "r" (&ret));
return ret; return ret;
} }
NOOPTIMIZE
int getpid(void) int getpid(void)
{ {
int ret = 0; volatile uint32_t ret = 0;
register uint32_t r1 __asm("r1") = (uint32_t)&ret; __asm volatile("\
__asm("\
mov r0, 2; \ mov r0, 2; \
mov r1, %0; \ mov r1, %0; \
mov r2, 0; \
svc 0; \ svc 0; \
" :: "r" (r1)); " :: "r" (&ret));
return ret; return ret;
} }
NOOPTIMIZE
void *sbrk(unsigned int bytes) void *sbrk(unsigned int bytes)
{ {
uint32_t ret = 0; volatile uint32_t ret = 0;
register uint32_t r1 __asm("r1") = bytes; uint32_t args[1] = { bytes };
register uint32_t r2 __asm("r2") = (uint32_t)&ret;
__asm("\ __asm("\
mov r0, 4; \ mov r0, 4; \
mov r1, %0; \ mov r1, %0; \
mov r2, %1; \ mov r2, %1; \
svc 0; \ svc 0; \
" :: "r" (r1), "r" (r2)); " :: "r" (&ret), "r" (args));
__asm("mov %0, r2" : "=r" (ret)); return (void *)ret;
return *((void **)ret);
} }
NOOPTIMIZE
int execve(const char *file, char * const argv[], char * const envp[]) int execve(const char *file, char * const argv[], char * const envp[])
{ {
volatile uint32_t ret = 0; volatile uint32_t ret = 0;
register uint32_t r1 __asm("r1") = (uint32_t)file; uint32_t args[3] = { (uint32_t)file, (uint32_t)argv, (uint32_t)envp };
register uint32_t r2 __asm("r2") = (uint32_t)argv;
register uint32_t r3 __asm("r3") = (uint32_t)envp;
register uint32_t r12 __asm("r12") = (uint32_t)&ret;
__asm("\ __asm("\
mov r0, 5; \ mov r0, 5; \
mov r1, %0; \ mov r1, %0; \
mov r2, %1; \ mov r2, %1; \
mov r3, %2; \
mov r12, %3; \
svc 0; \ svc 0; \
" :: "r" (r1), "r" (r2), "r" (r3), "r" (r12)); " :: "r" (&ret), "r" (args));
if (ret == (uint32_t)-1) if (ret == (uint32_t)-1)
return ret; return ret;
@ -77,110 +78,101 @@ int execve(const char *file, char * const argv[], char * const envp[])
// //
// Clock-related calls // Clock-related calls
NOOPTIMIZE
void delay(unsigned int ms) void delay(unsigned int ms)
{ {
register uint32_t r1 __asm("r1") = ms; uint32_t args[1] = { ms };
__asm("\ __asm("\
mov r0, 0; \ mov r0, 0; \
mov r1, %0; \ mov r1, 0; \
mov r2, %0; \
svc 2; \ svc 2; \
" :: "r" (r1)); " :: "r" (args));
} }
NOOPTIMIZE
unsigned int ticks(void) unsigned int ticks(void)
{ {
unsigned int ret = 0; volatile unsigned int ret = 0;
register uint32_t r1 __asm("r1") = (uint32_t)&ret;
__asm("\ __asm("\
mov r0, 2; \ mov r0, 2; \
mov r1, %0; \ mov r1, %0; \
mov r2, 0; \
svc 2; \ svc 2; \
" :: "r" (r1)); " :: "r" (&ret));
return ret; return ret;
} }
// //
// File-related calls // File-related calls
NOOPTIMIZE
int mount(vfs_volume_funcs *funcs, uint32_t flags) int mount(vfs_volume_funcs *funcs, uint32_t flags)
{ {
int ret = 0; volatile int ret = 0;
register uint32_t r1 __asm("r1") = (uint32_t)funcs; uint32_t args[2] = { (uint32_t)funcs, flags };
register uint32_t r2 __asm("r2") = flags;
register uint32_t r3 __asm("r3") = (uint32_t)&ret;
__asm("\ __asm("\
mov r0, 0; \ mov r0, 0; \
mov r1, %0; \ mov r1, %0; \
mov r2, %1; \ mov r2, %1; \
mov r3, %2; \
svc 3; \ svc 3; \
" :: "r" (r1), "r" (r2), "r" (r3)); " :: "r" (&ret), "r" (args));
return ret; return ret;
} }
NOOPTIMIZE
int open(const char *path, uint32_t flags) int open(const char *path, uint32_t flags)
{ {
int ret = 0; volatile int ret = 0;
register uint32_t r1 __asm("r1") = (uint32_t)path; uint32_t args[2] = { (uint32_t)path, flags };
register uint32_t r2 __asm("r2") = flags;
register uint32_t r3 __asm("r3") = (uint32_t)&ret;
__asm("\ __asm("\
mov r0, 1; \ mov r0, 1; \
mov r1, %0; \ mov r1, %0; \
mov r2, %1; \ mov r2, %1; \
mov r3, %2; \
svc 3; \ svc 3; \
" :: "r" (r1), "r" (r2), "r" (r3)); " :: "r" (&ret), "r" (args));
return ret; return ret;
} }
NOOPTIMIZE
int close(int fd) int close(int fd)
{ {
int ret = 0; volatile int ret = 0;
register uint32_t r1 __asm("r1") = fd; uint32_t args[1] = { fd };
register uint32_t r2 __asm("r2") = (uint32_t)&ret;
__asm("\ __asm("\
mov r0, 2; \ mov r0, 2; \
mov r1, %0; \ mov r1, %0; \
mov r2, %1; \ mov r2, %1; \
svc 3; \ svc 3; \
" :: "r" (r1), "r" (r2)); " :: "r" (&ret), "r" (args));
return ret; return ret;
} }
NOOPTIMIZE
int read(int fd, uint32_t count, uint8_t *buffer) int read(int fd, uint32_t count, uint8_t *buffer)
{ {
int ret = 0; volatile int ret = 0;
register uint32_t r1 __asm("r1") = fd; uint32_t args[3] = { fd, count, (uint32_t)buffer };
register uint32_t r2 __asm("r2") = count;
register uint32_t r3 __asm("r3") = (uint32_t)buffer;
register uint32_t r12 __asm("r12") = (uint32_t)&ret;
__asm("\ __asm("\
mov r0, 3; \ mov r0, 3; \
mov r1, %0; \ mov r1, %0; \
mov r2, %1; \ mov r2, %1; \
mov r3, %2; \
mov r12, %3; \
svc 3; \ svc 3; \
" :: "r" (r1), "r" (r2), "r" (r3), "r" (r12)); " :: "r" (&ret), "r" (args));
return ret; return ret;
} }
NOOPTIMIZE
int write(int fd, uint32_t count, const uint8_t *buffer) int write(int fd, uint32_t count, const uint8_t *buffer)
{ {
int ret = 0; volatile int ret = 0;
register uint32_t r1 __asm("r1") = fd; uint32_t args[3] = { fd, count, (uint32_t)buffer };
register uint32_t r2 __asm("r2") = count;
register uint32_t r3 __asm("r3") = (uint32_t)buffer;
register uint32_t r12 __asm("r12") = (uint32_t)&ret;
__asm("\ __asm("\
mov r0, 4; \ mov r0, 4; \
mov r1, %0; \ mov r1, %0; \
mov r2, %1; \ mov r2, %1; \
mov r3, %2; \
mov r12, %3; \
svc 3; \ svc 3; \
" :: "r" (r1), "r" (r2), "r" (r3), "r" (r12)); " :: "r" (&ret), "r" (args));
return ret; return ret;
} }

@ -0,0 +1,6 @@
#!/bin/bash
# Installs header files to arm-stmos directory (~/arm-stmos)
cp -Rv include/* $HOME/arm-stmos/usr/include
cp -Rv platform/stmos/include/* $HOME/arm-stmos/usr/include
Loading…
Cancel
Save