Skip to content

Commit b5ec8ba

Browse files
authored
Merge pull request #375 from Patater/make-svc-great-again
svc: Make svc_exports.h great again
2 parents 04458eb + a7ce44c commit b5ec8ba

File tree

3 files changed

+108
-83
lines changed

3 files changed

+108
-83
lines changed

api/inc/register_gateway.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@
1919

2020
#include "api/inc/register_gateway_exports.h"
2121
#include "api/inc/uvisor_exports.h"
22+
#include "api/inc/svc_exports.h"
2223
#include <stdint.h>
2324

2425
/** Get the offset of a struct member.
2526
* @internal
2627
*/
2728
#define __UVISOR_OFFSETOF(type, member) ((uint32_t) (&(((type *)(0))->member)))
2829

30+
/** Generate the SVCall opcode from the SVC ID. */
31+
#define UVISOR_SVC_OPCODE(id) ((uint16_t) (0xDF00 | ((id) & 0xFF)))
32+
2933
/** Generate the opcode of the 16-bit Thumb-2 16-bit T2 encoding of the branch
3034
* instruction.
3135
* @internal

api/inc/svc_exports.h

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright (c) 2013-2016, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
#ifndef __UVISOR_API_SVC_EXPORTS_H__
18+
#define __UVISOR_API_SVC_EXPORTS_H__
19+
20+
#include "api/inc/uvisor_exports.h"
21+
#include "api/inc/api.h"
22+
#include <stdint.h>
23+
24+
/* An SVCall takes a 8bit immediate, which is used as follows:
25+
*
26+
* For fast APIs:
27+
*
28+
* 7 6 5 4 3 2 1 0
29+
* .---.---.---.---.---.---.---.---.
30+
* | 1 | N | N | N | h | h | h | h |
31+
* '---'---'---'---'---'---'---'---'
32+
* | |_______| |___________|
33+
* | | |
34+
* | | |
35+
* | | |
36+
* | | (h) Handler index in the hardcoded table (max 16)
37+
* | |
38+
* | (N) Number of arguments to pass to the handler (max 4)
39+
* |
40+
* SVCall mode: Fast
41+
*
42+
* For slow APIs:
43+
*
44+
* 7 6 5 4 3 2 1 0
45+
* .---.---.---.---.---.---.---.---.
46+
* | 0 | c | c | c | c | c | c | c |
47+
* '---'---'---'---'---'---'---'---'
48+
* | |_______________________|
49+
* | |
50+
* | |
51+
* | (c) Handler index in the custom table (max 128)
52+
* |
53+
* SVCall mode: Slow
54+
*/
55+
56+
/* 1 bit of the SVCall imm8 field is used to determine the mode, fast/slow. */
57+
#define UVISOR_SVC_MODE_FAST 1
58+
#define UVISOR_SVC_MODE_SLOW 0
59+
#define UVISOR_SVC_MODE_BIT 7
60+
#define UVISOR_SVC_MODE_MASK ((uint8_t) (1 << UVISOR_SVC_MODE_BIT))
61+
#define UVISOR_SVC_MODE(mode) ((uint8_t) (((mode) << UVISOR_SVC_MODE_BIT) & UVISOR_SVC_MODE_MASK))
62+
63+
/* 7 or 4 bits of the SVCall imm8 field are used to determine the table index,
64+
* depending on the mode, fast/slow. */
65+
#define UVISOR_SVC_FAST_INDEX_MAX (1 << 4)
66+
#define UVISOR_SVC_SLOW_INDEX_MAX (1 << 7)
67+
#define UVISOR_SVC_FAST_INDEX_BIT 0
68+
#define UVISOR_SVC_FAST_INDEX_MASK ((uint8_t) (0xF << UVISOR_SVC_FAST_INDEX_BIT))
69+
#define UVISOR_SVC_SLOW_INDEX_BIT 0
70+
#define UVISOR_SVC_SLOW_INDEX_MASK ((uint8_t) (0x7F << UVISOR_SVC_SLOW_INDEX_BIT))
71+
#define UVISOR_SVC_FAST_INDEX(index) ((uint8_t) (((index) << UVISOR_SVC_FAST_INDEX_BIT) & UVISOR_SVC_FAST_INDEX_MASK))
72+
#define UVISOR_SVC_SLOW_INDEX(index) ((uint8_t) (((index) << UVISOR_SVC_SLOW_INDEX_BIT) & UVISOR_SVC_SLOW_INDEX_MASK))
73+
74+
/* When the SVC mode is "fast", the imm8 field also contains a specification of
75+
* the call interface (number of arguments).
76+
* This is needed for context switches, since the stack manipulation routines
77+
* need to know how many arguments to copy from source to destination. */
78+
#define UVISOR_SVC_FAST_NARGS_BIT 4
79+
#define UVISOR_SVC_FAST_NARGS_MASK ((uint8_t) (0x7 << UVISOR_SVC_FAST_NARGS_BIT))
80+
#define UVISOR_SVC_FAST_NARGS_SET(nargs) ((uint8_t) (((nargs) << UVISOR_SVC_FAST_NARGS_BIT) & UVISOR_SVC_FAST_NARGS_MASK))
81+
#define UVISOR_SVC_FAST_NARGS_GET(svc_id) (((uint8_t) (svc_id) & UVISOR_SVC_FAST_NARGS_MASK) >> UVISOR_SVC_FAST_NARGS_BIT)
82+
83+
/* Macros to build the SVCall imm8 field.
84+
* For slow APIs only the SVC handler index is needed.
85+
* For fast APIs the SVC handler index and the number of arguments are needed. */
86+
#define UVISOR_SVC_CUSTOM_TABLE(index) ((uint8_t) (UVISOR_SVC_MODE(UVISOR_SVC_MODE_SLOW) | \
87+
UVISOR_SVC_SLOW_INDEX(index)))
88+
#define UVISOR_SVC_FIXED_TABLE(index, nargs) ((uint8_t) (UVISOR_SVC_MODE(UVISOR_SVC_MODE_FAST) | \
89+
UVISOR_SVC_FAST_INDEX(index) | \
90+
UVISOR_SVC_FAST_NARGS_SET(nargs)))
91+
92+
/* SVC immediate values for hardcoded table (call from unprivileged) */
93+
#define UVISOR_SVC_ID_UNVIC_OUT UVISOR_SVC_FIXED_TABLE(0, 0)
94+
/* Deprecated: UVISOR_SVC_ID_CX_IN(nargs) UVISOR_SVC_FIXED_TABLE(1, nargs) */
95+
/* Deprecated: UVISOR_SVC_ID_CX_OUT UVISOR_SVC_FIXED_TABLE(2, 0) */
96+
#define UVISOR_SVC_ID_REGISTER_GATEWAY UVISOR_SVC_FIXED_TABLE(3, 0)
97+
#define UVISOR_SVC_ID_BOX_INIT_FIRST UVISOR_SVC_FIXED_TABLE(4, 0)
98+
#define UVISOR_SVC_ID_BOX_INIT_NEXT UVISOR_SVC_FIXED_TABLE(5, 0)
99+
100+
/* SVC immediate values for hardcoded table (call from privileged) */
101+
#define UVISOR_SVC_ID_UNVIC_IN UVISOR_SVC_FIXED_TABLE(0, 0)
102+
103+
#endif /* __UVISOR_API_SVC_EXPORTS_H__ */

core/system/inc/svc.h

Lines changed: 1 addition & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -17,89 +17,7 @@
1717
#ifndef __SVC_H__
1818
#define __SVC_H__
1919

20-
#include "api/inc/api.h"
21-
22-
/* An SVCall takes a 8bit immediate, which is used as follows:
23-
*
24-
* For fast APIs:
25-
*
26-
* 7 6 5 4 3 2 1 0
27-
* .---.---.---.---.---.---.---.---.
28-
* | 1 | N | N | N | h | h | h | h |
29-
* '---'---'---'---'---'---'---'---'
30-
* | |_______| |___________|
31-
* | | |
32-
* | | |
33-
* | | |
34-
* | | (h) Handler index in the hardcoded table (max 16)
35-
* | |
36-
* | (N) Number of arguments to pass to the handler (max 4)
37-
* |
38-
* SVCall mode: Fast
39-
*
40-
* For slow APIs:
41-
*
42-
* 7 6 5 4 3 2 1 0
43-
* .---.---.---.---.---.---.---.---.
44-
* | 0 | c | c | c | c | c | c | c |
45-
* '---'---'---'---'---'---'---'---'
46-
* | |_______________________|
47-
* | |
48-
* | |
49-
* | (c) Handler index in the custom table (max 128)
50-
* |
51-
* SVCall mode: Slow
52-
*/
53-
54-
/* 1 bit of the SVCall imm8 field is used to determine the mode, fast/slow. */
55-
#define UVISOR_SVC_MODE_FAST 1
56-
#define UVISOR_SVC_MODE_SLOW 0
57-
#define UVISOR_SVC_MODE_BIT 7
58-
#define UVISOR_SVC_MODE_MASK ((uint8_t) (1 << UVISOR_SVC_MODE_BIT))
59-
#define UVISOR_SVC_MODE(mode) ((uint8_t) (((mode) << UVISOR_SVC_MODE_BIT) & UVISOR_SVC_MODE_MASK))
60-
61-
/* 7 or 4 bits of the SVCall imm8 field are used to determine the table index,
62-
* depending on the mode, fast/slow. */
63-
#define UVISOR_SVC_FAST_INDEX_MAX (1 << 4)
64-
#define UVISOR_SVC_SLOW_INDEX_MAX (1 << 7)
65-
#define UVISOR_SVC_FAST_INDEX_BIT 0
66-
#define UVISOR_SVC_FAST_INDEX_MASK ((uint8_t) (0xF << UVISOR_SVC_FAST_INDEX_BIT))
67-
#define UVISOR_SVC_SLOW_INDEX_BIT 0
68-
#define UVISOR_SVC_SLOW_INDEX_MASK ((uint8_t) (0x7F << UVISOR_SVC_SLOW_INDEX_BIT))
69-
#define UVISOR_SVC_FAST_INDEX(index) ((uint8_t) (((index) << UVISOR_SVC_FAST_INDEX_BIT) & UVISOR_SVC_FAST_INDEX_MASK))
70-
#define UVISOR_SVC_SLOW_INDEX(index) ((uint8_t) (((index) << UVISOR_SVC_SLOW_INDEX_BIT) & UVISOR_SVC_SLOW_INDEX_MASK))
71-
72-
/* When the SVC mode is "fast", the imm8 field also contains a specification of
73-
* the call interface (number of arguments).
74-
* This is needed for context switches, since the stack manipulation routines
75-
* need to know how many arguments to copy from source to destination. */
76-
#define UVISOR_SVC_FAST_NARGS_BIT 4
77-
#define UVISOR_SVC_FAST_NARGS_MASK ((uint8_t) (0x7 << UVISOR_SVC_FAST_NARGS_BIT))
78-
#define UVISOR_SVC_FAST_NARGS_SET(nargs) ((uint8_t) (((nargs) << UVISOR_SVC_FAST_NARGS_BIT) & UVISOR_SVC_FAST_NARGS_MASK))
79-
#define UVISOR_SVC_FAST_NARGS_GET(svc_id) (((uint8_t) (svc_id) & UVISOR_SVC_FAST_NARGS_MASK) >> UVISOR_SVC_FAST_NARGS_BIT)
80-
81-
/* Macros to build the SVCall imm8 field.
82-
* For slow APIs only the SVC handler index is needed.
83-
* For fast APIs the SVC handler index and the number of arguments are needed. */
84-
#define UVISOR_SVC_CUSTOM_TABLE(index) ((uint8_t) (UVISOR_SVC_MODE(UVISOR_SVC_MODE_SLOW) | \
85-
UVISOR_SVC_SLOW_INDEX(index)))
86-
#define UVISOR_SVC_FIXED_TABLE(index, nargs) ((uint8_t) (UVISOR_SVC_MODE(UVISOR_SVC_MODE_FAST) | \
87-
UVISOR_SVC_FAST_INDEX(index) | \
88-
UVISOR_SVC_FAST_NARGS_SET(nargs)))
89-
90-
/* SVC immediate values for hardcoded table (call from unprivileged) */
91-
#define UVISOR_SVC_ID_UNVIC_OUT UVISOR_SVC_FIXED_TABLE(0, 0)
92-
/* Deprecated: UVISOR_SVC_ID_CX_IN(nargs) UVISOR_SVC_FIXED_TABLE(1, nargs) */
93-
/* Deprecated: UVISOR_SVC_ID_CX_OUT UVISOR_SVC_FIXED_TABLE(2, 0) */
94-
#define UVISOR_SVC_ID_REGISTER_GATEWAY UVISOR_SVC_FIXED_TABLE(3, 0)
95-
#define UVISOR_SVC_ID_BOX_INIT_FIRST UVISOR_SVC_FIXED_TABLE(4, 0)
96-
#define UVISOR_SVC_ID_BOX_INIT_NEXT UVISOR_SVC_FIXED_TABLE(5, 0)
97-
98-
/* SVC immediate values for hardcoded table (call from privileged) */
99-
#define UVISOR_SVC_ID_UNVIC_IN UVISOR_SVC_FIXED_TABLE(0, 0)
100-
101-
/** Generate the SVCall opcode from the SVC ID. */
102-
#define UVISOR_SVC_OPCODE(id) ((uint16_t) 0xDF00 | (uint8_t) ((id) & 0xFF))
20+
#include "api/inc/svc_exports.h"
10321

10422
#define UVISOR_SVC_ID_GET(target) UVISOR_SVC_CUSTOM_TABLE(offsetof(UvisorSvcTarget, target) / sizeof(uint32_t))
10523

0 commit comments

Comments
 (0)