Callisto4Bootloader: lib/c4_privilege_modes.c Source File

Callisto4Bootloader

c4_privilege_modes.c
Go to the documentation of this file.
1
13 /*******************************************************************************
14 * Includes
15 ******************************************************************************/
16 #include "c4_privilege_modes.h"
17
18 #ifdef DEBUG
19 # include "fsl_debug_console.h"
20 #else
21 # define PRINTF(f, ...)
22 #endif
23
24 #include <stdarg.h>
25 #include <stdio.h>
26
27 /*******************************************************************************
28 * Defines
29 ******************************************************************************/
30
31 #define C4_SVC_PRINT_CALL(str,l) \
32 asm("MOV r0, %0\n\tMOV r1, %1\n\tSVC %2\n\t":: \
33 "r"(str), "r"(l), "I" (C4_SVC_PRINT))
34
36 #define MAX_PRINT 128
37
38 /*******************************************************************************
39 * Structures
40 ******************************************************************************/
41
42 /*******************************************************************************
43 * Prototypes
44 ******************************************************************************/
45
46 /*******************************************************************************
47 * Variables
48 ******************************************************************************/
49
50 /*******************************************************************************
51 * Interface Implementation
52 ******************************************************************************/
53
54 void C4_enterUnprivileged()
55 {
56 CONTROL_Type ctrlReg;
57 printf("In %s\n", __PRETTY_FUNCTION__);
58
59 asm volatile ("MRS %0, CONTROL\n\t":"=r"(ctrlReg));
60
61 ctrlReg.b.nPRIV = 1;
62
63 asm volatile ("MSR CONTROL, %0\n\t"::"r"(ctrlReg));
64 }
65
66 /*******************************************************************************/
67
68 void *C4_executePrivileged(C4_SVC_Op_t op, ...)
69 {
70 switch (op)
71 {
72 case C4_SVC_PRINT:
73 {
74 char printString[MAX_PRINT] = "";
75 int len = 0;
76
77 va_list args;
78 va_start(args, op);
79 char *fmtString = va_arg(args, char *);
80
81 len = vsnprintf(printString, MAX_PRINT, fmtString, args);
82
83 if (len <= MAX_PRINT)
84 {
85 C4_SVC_PRINT_CALL(printString, len);
86 }
87 else
88 {
89 C4_SVC_PRINT_CALL(printString, MAX_PRINT);
90 len = snprintf(printString, MAX_PRINT,
91 " ...\n\tERROR Print string too long %d > %d\n",
92 len, MAX_PRINT);
93 C4_SVC_PRINT_CALL(printString, len);
94 }
95 va_end(args);
96 break;
97 }
98 case C4_SVC_ADD:
99 {
100 va_list args;
101 va_start(args, op);
102 int a1 = va_arg(args, int);
103 int a2 = va_arg(args, int);
104 int res = 0;
105 asm("MOV r0, %1\n\tMOV r1, %2\n\tSVC %3\n\tMOV %0, r0\n\t":"=r"(res): \
106 "r"(a1), "r"(a2), "I" (C4_SVC_ADD));
107 va_end(args);
108 return (void *)res;
109 break;
110 }
111 break;
112 case C4_SVC_USER:
113 break;
114
115 default:
116 {
117 // unhandled op
118 char printString[MAX_PRINT] = "";
119 int len = 0;
120 len = snprintf(printString, MAX_PRINT,
121 " Unhandled operation '%d' in %s\n",
122 (int)op, __FUNCTION__);
123 C4_SVC_PRINT_CALL(printString, len);
124 }
125 }
126 return NULL;
127 }
C4_executePrivileged
void * C4_executePrivileged(C4_SVC_Op_t op,...)
Execute operation in privileged mode.
Definition: c4_privilege_modes.c:62
NULL
#define NULL
Definition: bootloader_common.h:26
CONTROL_Type::b
struct CONTROL_Type::@3 b
CONTROL_Type::nPRIV
uint32_t nPRIV
Definition: core_armv81mml.h:426
C4_enterUnprivileged
void C4_enterUnprivileged()
Set the CONTROL register for Unprivileged level.
Definition: c4_privilege_modes.c:48
C4_SVC_PRINT_CALL
#define C4_SVC_PRINT_CALL(str, l)
Definition: c4_privilege_modes.c:29
C4_SVC_Op_t
C4_SVC_Op_t
Enumeration of operations performed by SVC. The operation IDs need to fit into a uint8_t.
Definition: c4_privilege_modes.h:99
CONTROL_Type
Union type to access the Control Registers (CONTROL).
Definition: core_armv81mml.h:422
MAX_PRINT
#define MAX_PRINT
Length of string for printf.
Definition: c4_privilege_modes.c:34
c4_privilege_modes.h
Provide mechanisms to specify which stack to use and for the processor to operate in Unprivileged mod...