From 04548126b5c430356a723e30ba222ff4b874fad0 Mon Sep 17 00:00:00 2001 From: tcsullivan Date: Tue, 25 Sep 2018 17:09:53 -0400 Subject: [PATCH] began work to add privilege --- include/priv_gpio.h | 26 ++++++++++++++++++++++ src/gpio.c | 21 ++++++++++++++++++ src/main.c | 21 +++++++++++++----- src/svc.c | 53 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+), 5 deletions(-) create mode 100644 include/priv_gpio.h create mode 100644 src/svc.c diff --git a/include/priv_gpio.h b/include/priv_gpio.h new file mode 100644 index 0000000..eee5e18 --- /dev/null +++ b/include/priv_gpio.h @@ -0,0 +1,26 @@ +#ifndef PRIV_GPIO_H_ +#define PRIV_GPIO_H_ + +#define GPIO_MODE 0 +#define GPIO_TYPE 1 +#define GPIO_PUPD 2 +#define GPIO_SPEED 3 +#define GPIO_OUT 4 + +void gpio(uint32_t call, uint32_t port, uint32_t pin, uint32_t value) +{ + register uint32_t r0 asm("r0") = call; + register uint32_t r1 asm("r1") = port; + register uint32_t r2 asm("r2") = pin; + register uint32_t r3 asm("r3") = value; + + asm("\ + mov r0, %0; \ + mov r1, %1; \ + mov r2, %2; \ + mov r3, %3; \ + svc 1; \ + " :: "r" (r0), "r" (r1), "r" (r2), "r" (r3)); +} + +#endif // PRIV_GPIO_H_ diff --git a/src/gpio.c b/src/gpio.c index 151779d..29ed763 100644 --- a/src/gpio.c +++ b/src/gpio.c @@ -20,6 +20,27 @@ #include +void gpio_svc(uint32_t *args) +{ + switch (args[0]) { + case 0: + gpio_mode((GPIO_TypeDef *)args[1], args[2], args[3]); + break; + case 1: + gpio_type((GPIO_TypeDef *)args[1], args[2], args[3]); + break; + case 2: + gpio_pupd((GPIO_TypeDef *)args[1], args[2], args[3]); + break; + case 3: + gpio_speed((GPIO_TypeDef *)args[1], args[2], args[3]); + break; + case 4: + gpio_dout((GPIO_TypeDef *)args[1], args[2], args[3]); + break; + } +} + void gpio_init(void) { // enable clocks diff --git a/src/main.c b/src/main.c index 8028f93..c929d16 100644 --- a/src/main.c +++ b/src/main.c @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -53,19 +54,29 @@ int main(void) void task2(void); void kmain(void) { - gpio_mode(GPIOA, 5, OUTPUT); + gpio(GPIO_MODE, (uint32_t)GPIOA, 5, OUTPUT); + //gpio_mode(GPIOA, 5, OUTPUT); task_start(task2, 512); for (int i = 0; i < 8; i++) { gpio_dout(GPIOA, 5, !(i & 1)); delay(200); } - - return; } +void task3(void); void task2(void) { - while (1) - delay(800); + delay(400); + task_start(task3, 1024); +} + +void task3(void) +{ + int state = 0; + delay(2500); + while (1) { + gpio_dout(GPIOA, 5, state ^= 1); + delay(500); + } } diff --git a/src/svc.c b/src/svc.c new file mode 100644 index 0000000..4342a95 --- /dev/null +++ b/src/svc.c @@ -0,0 +1,53 @@ +/** + * @file svc.c + * An unused handler for SVC calls + * TODO: use SVC calls, possibly allowing for switch to unprivileged mode? + * + * Copyright (C) 2018 Clyne Sullivan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include + +extern void gpio_svc(uint32_t *); + +void svc_handler(uint32_t *args) +{ + uint32_t svc_number = ((char *)args[6])[-2]; + + switch (svc_number) { + case 1: + gpio_svc(args); + break; + default: + break; + } +} + +void SVC_Handler(void) { + uint32_t *args; + + asm("\ + tst lr, #4; \ + ite eq; \ + mrseq %0, msp; \ + mrsne %0, psp; \ + " : "=r" (args)); + + svc_handler(args); +} +