Skip to content

Commit

Permalink
hal: add armv8m33-nrf9160 port
Browse files Browse the repository at this point in the history
JIRA: RTOS-283
  • Loading branch information
damianloew committed Jun 27, 2023
1 parent 2571d96 commit eb5af36
Show file tree
Hide file tree
Showing 25 changed files with 2,678 additions and 0 deletions.
13 changes: 13 additions & 0 deletions hal/armv8m/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#
# Makefile for Phoenix-RTOS kernel (ARMv8M HAL)
#
# Copyright 2022 Phoenix Systems
#


ifneq (, $(findstring nrf, $(TARGET_SUBFAMILY)))
include hal/armv8m/nrf/Makefile
CFLAGS += -Ihal/armv8m/nrf -Ihal/armv8m
endif

OBJS += $(addprefix $(PREFIX_O)hal/armv8m/, string.o spinlock.o cpu.o hal.o pmap.o exceptions.o)
231 changes: 231 additions & 0 deletions hal/armv8m/arch/cpu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
/*
* Phoenix-RTOS
*
* Operating system kernel
*
* CPU related routines
*
* Copyright 2014, 2017, 2022 Phoenix Systems
* Author: Jacek Popko, Pawel Pisarczyk, Aleksander Kaminski, Damian Loewnau
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/

#ifndef _HAL_ARMV8M_CPU_H_
#define _HAL_ARMV8M_CPU_H_


#if defined(CPU_NRF9160)
#define CPU_NRF91
#endif

#include "types.h"

#define SIZE_PAGE 0x200

#ifndef SIZE_USTACK
#define SIZE_USTACK (3 * SIZE_PAGE)
#endif

#ifndef SIZE_KSTACK
#define SIZE_KSTACK (4 * SIZE_PAGE)
#endif

#define RET_HANDLER_MSP 0xfffffff1u
#define RET_THREAD_MSP 0xfffffff9u
#define RET_THREAD_PSP 0xfffffffdu
#define HWCTXSIZE 8
#define USERCONTROL 0x3u

#ifndef __ASSEMBLY__


#define SYSTICK_INTERVAL 1000


#define PUTONSTACK(kstack, t, v) \
do { \
(kstack) -= (sizeof(t) + 3) & ~0x3u; \
*((t *)kstack) = (v); \
} while (0)


#define GETFROMSTACK(ustack, t, v, n) \
do { \
ustack = (void *)(((ptr_t)ustack + sizeof(t) - 1) & ~(sizeof(t) - 1)); \
(v) = *(t *)ustack; \
ustack += (sizeof(t) + 3) & ~0x3u; \
} while (0)

typedef struct _cpu_context_t {
u32 savesp_s;
u32 padding;

/* Saved by ISR */
u32 psp_s;
u32 r4;
u32 r5;
u32 r6;
u32 r7;
u32 r8;
u32 r9;
u32 r10;
u32 r11;
u32 irq_ret;

/* Saved by hardware */
u32 r0;
u32 r1;
u32 r2;
u32 r3;
u32 r12;
u32 lr;
u32 pc;
u32 psr;

} cpu_context_t;


static inline void hal_cpuDisableInterrupts(void)
{
__asm__ volatile("cpsid if");
}


static inline void hal_cpuEnableInterrupts(void)
{
__asm__ volatile("cpsie if");
}


static inline void hal_cpuHalt(void)
{
}


/* bit operations */


static inline unsigned int hal_cpuGetLastBit(unsigned long v)
{
int pos;
/* clang-format off */
__asm__ volatile("clz %0, %1" : "=r" (pos) : "r" (v));

return 31 - pos;
}


static inline unsigned int hal_cpuGetFirstBit(unsigned long v)
{
unsigned pos;

__asm__ volatile("\
rbit %0, %1; \
clz %0, %0;" : "=r" (pos) : "r" (v));

return pos;
}


/* context management */

static inline void hal_cpuSetCtxGot(cpu_context_t *ctx, void *got)
{
ctx->r9 = (u32)got;
}


static inline void hal_cpuSetGot(void *got)
{
__asm__ volatile("mov r9, %0" :: "r" (got));
}


static inline void *hal_cpuGetGot(void)
{
void *got;

__asm__ volatile("mov %0, r9" : "=r" (got));

return got;
}


static inline void hal_cpuRestore(cpu_context_t *curr, cpu_context_t *next)
{
curr->savesp_s = (u32)next;
}


static inline void hal_cpuSetReturnValue(cpu_context_t *ctx, int retval)
{
ctx->r0 = retval;
}


static inline u32 hal_cpuGetPC(void)
{
void *pc;

__asm__ volatile("mov %0, pc" : "=r" (pc));
/* clang-format on */

return (u32)pc;
}


static inline void _hal_cpuSetKernelStack(void *kstack)
{
}


static inline void *hal_cpuGetSP(cpu_context_t *ctx)
{
return (void *)ctx;
}


static inline void *hal_cpuGetUserSP(cpu_context_t *ctx)
{
return (void *)ctx->psp_s;
}


static inline int hal_cpuSupervisorMode(cpu_context_t *ctx)
{
return ((ctx->irq_ret & (1 << 2)) == 0) ? 1 : 0;
}


static inline int hal_cpuPushSignal(void *kstack, void (*handler)(void), int sig)
{
return 0;
}


/* core management */


static inline unsigned int hal_cpuGetID(void)
{
return 0;
}


static inline unsigned int hal_cpuGetCount(void)
{
return 1;
}


static inline void cpu_sendIPI(unsigned int cpu, unsigned int intr)
{
}


#endif

#endif
52 changes: 52 additions & 0 deletions hal/armv8m/arch/exceptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Phoenix-RTOS
*
* Operating system kernel
*
* Exceptions handling
*
* Copyright 2022 Phoenix Systems
* Author: Damian Loewnau
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/

#ifndef _HAL_ARMV8M_EXCEPTIONS_H_
#define _HAL_ARMV8M_EXCEPTIONS_H_

#include "types.h"

#define EXC_DEFAULT 128

#define EXC_UNDEFINED 3

#define SIZE_CTXDUMP 512 /* Size of dumped context */


typedef struct _exc_context_t {
/* Saved by ISR */
u32 psp_s;
u32 r4;
u32 r5;
u32 r6;
u32 r7;
u32 r8;
u32 r9;
u32 r10;
u32 r11;
u32 excret;

/* Saved by hardware */
u32 r0;
u32 r1;
u32 r2;
u32 r3;
u32 r12;
u32 lr;
u32 pc;
u32 psr;
} exc_context_t;

#endif
38 changes: 38 additions & 0 deletions hal/armv8m/arch/interrupts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Phoenix-RTOS
*
* Operating system kernel
*
* Interrupt handling
*
* Copyright 2016, 2017, 2020, 2022 Phoenix Systems
* Author: Pawel Pisarczyk, Artur Wodejko, Hubert Buczynski, Damian Loewnau
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/


#ifndef _HAL_ARMV8M_INTERRUPTS_H_
#define _HAL_ARMV8M_INTERRUPTS_H_

#include "cpu.h"

#define SVC_IRQ 11
#define PENDSV_IRQ 14
#define SYSTICK_IRQ 15


typedef struct _intr_handler_t {
struct _intr_handler_t *next;
struct _intr_handler_t *prev;
/* irq */
unsigned int n;
/* handler function */
int (*f)(unsigned int, cpu_context_t *, void *);
void *data;
void *got;
} intr_handler_t;

#endif
61 changes: 61 additions & 0 deletions hal/armv8m/arch/pmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Phoenix-RTOS
*
* Operating system kernel
*
* pmap interface - machine dependent part of VM subsystem (ARMv8)
*
* Copyright 2017, 2020, 2022 Phoenix Systems
* Author: Pawel Pisarczyk, Aleksander Kaminski, Hubert Buczynski, Damian Loewnau
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/

#ifndef _HAL_PMAP_ARMV8M_H_
#define _HAL_PMAP_ARMV8M_H_

#include "types.h"

#define PGHD_PRESENT 0x01u
#define PGHD_USER 0x04u
#define PGHD_WRITE 0x02u
#define PGHD_EXEC 0x00u
#define PGHD_DEV 0x00u
#define PGHD_NOT_CACHED 0x00u

/* Page flags */
#define PAGE_FREE 0x00000001u

#define PAGE_OWNER_BOOT (0u << 1)
#define PAGE_OWNER_KERNEL (1u << 1)
#define PAGE_OWNER_APP (2u << 1)

#define PAGE_KERNEL_SYSPAGE (1u << 4)
#define PAGE_KERNEL_CPU (2u << 4)
#define PAGE_KERNEL_PTABLE (3u << 4)
#define PAGE_KERNEL_PMAP (4u << 4)
#define PAGE_KERNEL_STACK (5u << 4)
#define PAGE_KERNEL_HEAP (6u << 4)


#ifndef __ASSEMBLY__

typedef struct _page_t {
addr_t addr;
u8 idx;
u16 flags;
struct _page_t *next;
} page_t;


typedef struct _pmap_t {
u32 mpr;
void *start;
void *end;
} pmap_t;

#endif

#endif
Loading

0 comments on commit eb5af36

Please sign in to comment.