Skip to content

Commit 670d16a

Browse files
committed
Replace SVC IDs with offsetof in structure
1 parent 9500eba commit 670d16a

File tree

4 files changed

+56
-72
lines changed

4 files changed

+56
-72
lines changed

core/debug/src/debug.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ void debug_fault(THaltError reason, uint32_t lr, uint32_t sp)
158158

159159
static void debug_die(void)
160160
{
161-
UVISOR_SVC(UVISOR_SVC_ID_HALT_USER_ERR, "", DEBUG_BOX_HALT);
161+
UVISOR_SVC(UVISOR_SVC_ID_GET(error), "", DEBUG_BOX_HALT);
162162
}
163163

164164
void debug_reboot(TResetReason reason)

core/system/inc/svc.h

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -87,28 +87,6 @@
8787
UVISOR_SVC_FAST_INDEX(index) | \
8888
UVISOR_SVC_FAST_NARGS_SET(nargs)))
8989

90-
/* SVC immediate values for custom table */
91-
#define UVISOR_SVC_ID_ISR_SET UVISOR_SVC_CUSTOM_TABLE(1)
92-
#define UVISOR_SVC_ID_ISR_GET UVISOR_SVC_CUSTOM_TABLE(2)
93-
#define UVISOR_SVC_ID_IRQ_ENABLE UVISOR_SVC_CUSTOM_TABLE(3)
94-
#define UVISOR_SVC_ID_IRQ_DISABLE UVISOR_SVC_CUSTOM_TABLE(4)
95-
#define UVISOR_SVC_ID_IRQ_PEND_CLR UVISOR_SVC_CUSTOM_TABLE(5)
96-
#define UVISOR_SVC_ID_IRQ_PEND_SET UVISOR_SVC_CUSTOM_TABLE(6)
97-
#define UVISOR_SVC_ID_IRQ_PEND_GET UVISOR_SVC_CUSTOM_TABLE(7)
98-
#define UVISOR_SVC_ID_IRQ_PRIO_SET UVISOR_SVC_CUSTOM_TABLE(8)
99-
#define UVISOR_SVC_ID_IRQ_PRIO_GET UVISOR_SVC_CUSTOM_TABLE(9)
100-
#define UVISOR_SVC_ID_HALT_USER_ERR UVISOR_SVC_CUSTOM_TABLE(13)
101-
#define UVISOR_SVC_ID_IRQ_LEVEL_GET UVISOR_SVC_CUSTOM_TABLE(14)
102-
#define UVISOR_SVC_ID_BOX_ID_SELF UVISOR_SVC_CUSTOM_TABLE(15)
103-
#define UVISOR_SVC_ID_BOX_ID_CALLER UVISOR_SVC_CUSTOM_TABLE(16)
104-
#define UVISOR_SVC_ID_BOX_NAMESPACE_FROM_ID UVISOR_SVC_CUSTOM_TABLE(17)
105-
#define UVISOR_SVC_ID_DEBUG_REBOOT UVISOR_SVC_CUSTOM_TABLE(18)
106-
#define UVISOR_SVC_ID_DEBUG_REGISTER_BOX UVISOR_SVC_CUSTOM_TABLE(19)
107-
#define UVISOR_SVC_ID_IRQ_DISABLE_ALL UVISOR_SVC_CUSTOM_TABLE(20)
108-
#define UVISOR_SVC_ID_IRQ_ENABLE_ALL UVISOR_SVC_CUSTOM_TABLE(21)
109-
#define UVISOR_SVC_ID_PAGE_MALLOC UVISOR_SVC_CUSTOM_TABLE(22)
110-
#define UVISOR_SVC_ID_PAGE_FREE UVISOR_SVC_CUSTOM_TABLE(23)
111-
11290
/* SVC immediate values for hardcoded table (call from unprivileged) */
11391
#define UVISOR_SVC_ID_UNVIC_OUT UVISOR_SVC_FIXED_TABLE(0, 0)
11492
/* Deprecated: UVISOR_SVC_ID_CX_IN(nargs) UVISOR_SVC_FIXED_TABLE(1, nargs) */
@@ -123,6 +101,34 @@
123101
/** Generate the SVCall opcode from the SVC ID. */
124102
#define UVISOR_SVC_OPCODE(id) ((uint16_t) 0xDF00 | (uint8_t) ((id) & 0xFF))
125103

104+
#define UVISOR_SVC_ID_GET(target) UVISOR_SVC_CUSTOM_TABLE(offsetof(UvisorSvcTarget, target) / sizeof(uint32_t))
105+
106+
typedef struct {
107+
void (*not_implemented)(void);
108+
109+
void (*irq_enable)(uint32_t irqn);
110+
void (*irq_disable)(uint32_t irqn);
111+
void (*irq_disable_all)(void);
112+
void (*irq_enable_all)(void);
113+
void (*irq_set_vector)(uint32_t irqn, uint32_t vector);
114+
uint32_t (*irq_get_vector)(uint32_t irqn);
115+
void (*irq_set_priority)(uint32_t irqn, uint32_t priority);
116+
uint32_t (*irq_get_priority)(uint32_t irqn);
117+
void (*irq_set_pending)(uint32_t irqn);
118+
uint32_t (*irq_get_pending)(uint32_t irqn);
119+
void (*irq_clear_pending)(uint32_t irqn);
120+
int (*irq_get_level)(void);
121+
void (*irq_system_reset)(TResetReason reason);
122+
123+
int (*page_malloc)(UvisorPageTable * const table);
124+
int (*page_free)(const UvisorPageTable * const table);
125+
126+
int (*box_namespace)(int box_id, char *box_namespace, size_t length);
127+
128+
void (*debug_init)(const TUvisorDebugDriver * const driver);
129+
void (*error)(THaltUserError reason);
130+
} UVISOR_PACKED UvisorSvcTarget;
131+
126132
/* macro to execute an SVCall; additional metadata can be provided, which will
127133
* be appended right after the svc instruction */
128134
/* note: the macro is implicitly overloaded to allow 0 to 4 32bits arguments */

core/system/src/api.c

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
asm volatile( \
3333
"svc %[svc_id] \n" \
3434
"bx lr \n" \
35-
:: [svc_id] "I" (UVISOR_SVC_ID_ ## fn_name & 0xFF) \
35+
:: [svc_id] "I" (UVISOR_SVC_ID_GET(fn_target)) \
3636
); \
3737
__builtin_unreachable(); \
3838
}
@@ -47,25 +47,6 @@
4747
__builtin_unreachable(); \
4848
}
4949

50-
/* Use this one weird trick to map functions to SVC IDs. */
51-
#define UVISOR_SVC_ID_debug_register_driver UVISOR_SVC_ID_DEBUG_REGISTER_BOX
52-
#define UVISOR_SVC_ID_halt_user_error UVISOR_SVC_ID_HALT_USER_ERR
53-
#define UVISOR_SVC_ID_vmpu_box_namespace_from_id UVISOR_SVC_ID_BOX_NAMESPACE_FROM_ID
54-
#define UVISOR_SVC_ID_page_allocator_malloc UVISOR_SVC_ID_PAGE_MALLOC
55-
#define UVISOR_SVC_ID_page_allocator_free UVISOR_SVC_ID_PAGE_FREE
56-
#define UVISOR_SVC_ID_unvic_isr_set UVISOR_SVC_ID_ISR_SET
57-
#define UVISOR_SVC_ID_unvic_isr_get UVISOR_SVC_ID_ISR_GET
58-
#define UVISOR_SVC_ID_unvic_irq_enable UVISOR_SVC_ID_IRQ_ENABLE
59-
#define UVISOR_SVC_ID_unvic_irq_disable UVISOR_SVC_ID_IRQ_DISABLE
60-
#define UVISOR_SVC_ID_unvic_irq_pending_clr UVISOR_SVC_ID_IRQ_PEND_CLR
61-
#define UVISOR_SVC_ID_unvic_irq_pending_set UVISOR_SVC_ID_IRQ_PEND_SET
62-
#define UVISOR_SVC_ID_unvic_irq_pending_get UVISOR_SVC_ID_IRQ_PEND_GET
63-
#define UVISOR_SVC_ID_unvic_irq_priority_set UVISOR_SVC_ID_IRQ_PRIO_SET
64-
#define UVISOR_SVC_ID_unvic_irq_priority_get UVISOR_SVC_ID_IRQ_PRIO_GET
65-
#define UVISOR_SVC_ID_unvic_irq_level_get UVISOR_SVC_ID_IRQ_LEVEL_GET
66-
#define UVISOR_SVC_ID_unvic_irq_disable_all UVISOR_SVC_ID_IRQ_DISABLE_ALL
67-
#define UVISOR_SVC_ID_unvic_irq_enable_all UVISOR_SVC_ID_IRQ_ENABLE_ALL
68-
#define UVISOR_SVC_ID_debug_reboot UVISOR_SVC_ID_DEBUG_REBOOT
6950

7051
transition_np_to_p(debug_init, void, debug_register_driver, const TUvisorDebugDriver * const driver);
7152
transition_np_to_p(irq_system_reset, void, debug_reboot, TResetReason reason);

core/system/src/svc.c

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,30 @@ void __svc_not_implemented(void)
3737
}
3838

3939
/* SVC handlers */
40-
const void * const g_svc_vtor_tbl[] = {
41-
__svc_not_implemented, // 0
42-
unvic_isr_set, // 1
43-
unvic_isr_get, // 2
44-
unvic_irq_enable, // 3
45-
unvic_irq_disable, // 4
46-
unvic_irq_pending_clr, // 5
47-
unvic_irq_pending_set, // 6
48-
unvic_irq_pending_get, // 7
49-
unvic_irq_priority_set, // 8
50-
unvic_irq_priority_get, // 9
51-
__svc_not_implemented, // 10 Deprecated: benchmark_configure
52-
__svc_not_implemented, // 11 Deprecated: benchmark_start
53-
__svc_not_implemented, // 12 Deprecated: benchmark_stop
54-
halt_user_error, // 13
55-
unvic_irq_level_get, // 14
56-
__svc_not_implemented, // 15 Deprecated: vmpu_box_id_self
57-
__svc_not_implemented, // 16 Deprecated: vmpu_box_id_caller
58-
vmpu_box_namespace_from_id, // 17
59-
debug_reboot, // 18
60-
/* FIXME: This function will be made automatic when the debug box ACL is
61-
* introduced. The initialization will happen at uVisor boot time. */
62-
debug_register_driver, // 19
63-
unvic_irq_disable_all, // 20
64-
unvic_irq_enable_all, // 21
65-
page_allocator_malloc, // 22
66-
page_allocator_free, // 23
40+
const UvisorSvcTarget g_svc_vtor_tbl = {
41+
.not_implemented = __svc_not_implemented,
42+
43+
.irq_enable = unvic_irq_enable,
44+
.irq_disable = unvic_irq_disable,
45+
.irq_disable_all = unvic_irq_disable_all,
46+
.irq_enable_all = unvic_irq_enable_all,
47+
.irq_set_vector = unvic_isr_set,
48+
.irq_get_vector = unvic_isr_get,
49+
.irq_set_pending = unvic_irq_pending_set,
50+
.irq_get_pending = unvic_irq_pending_get,
51+
.irq_clear_pending = unvic_irq_pending_clr,
52+
.irq_set_priority = unvic_irq_priority_set,
53+
.irq_get_priority = unvic_irq_priority_get,
54+
.irq_get_level = unvic_irq_level_get,
55+
.irq_system_reset = debug_reboot,
56+
57+
.page_malloc = page_allocator_malloc,
58+
.page_free = page_allocator_free,
59+
60+
.box_namespace = vmpu_box_namespace_from_id,
61+
62+
.debug_init = debug_register_driver,
63+
.error = halt_user_error,
6764
};
6865

6966
/*******************************************************************************
@@ -246,7 +243,7 @@ void UVISOR_NAKED SVCall_IRQn_Handler(void)
246243

247244
:: [svc_mode_mask] "I" ((UVISOR_SVC_MODE_MASK) & 0xFF),
248245
[svc_fast_index_mask] "I" ((UVISOR_SVC_FAST_INDEX_MASK) & 0xFF),
249-
[svc_vtor_tbl_count] "i" (UVISOR_ARRAY_COUNT(g_svc_vtor_tbl) - 1),
246+
[svc_vtor_tbl_count] "i" (sizeof(g_svc_vtor_tbl) / sizeof(uint32_t) - 1),
250247
[priv_svc_0] "m" (g_priv_sys_hooks.priv_svc_0)
251248
);
252249
}
@@ -262,5 +259,5 @@ void svc_init(void)
262259
/* sanity checks */
263260
assert((&jump_table_unpriv_end - &jump_table_unpriv) == UVISOR_SVC_FAST_INDEX_MAX);
264261
assert((&jump_table_priv_end - &jump_table_priv) == UVISOR_SVC_FAST_INDEX_MAX);
265-
assert(UVISOR_ARRAY_COUNT(g_svc_vtor_tbl) <= UVISOR_SVC_SLOW_INDEX_MAX);
262+
assert(sizeof(g_svc_vtor_tbl) / sizeof(uint32_t) <= UVISOR_SVC_SLOW_INDEX_MAX);
266263
}

0 commit comments

Comments
 (0)