1 /*
2 * Copyright (c) 2023 Institute of Parallel And Distributed Systems (IPADS), Shanghai Jiao Tong University (SJTU)
3 * Licensed under the Mulan PSL v2.
4 * You can use this software according to the terms and conditions of the Mulan PSL v2.
5 * You may obtain a copy of Mulan PSL v2 at:
6 * http://license.coscl.org.cn/MulanPSL2
7 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9 * PURPOSE.
10 * See the Mulan PSL v2 for more details.
11 */
12 #ifndef OBJECT_CAP_GROUP_H
13 #define OBJECT_CAP_GROUP_H
14
15 #include <object/object.h>
16 #include <common/list.h>
17 #include <common/types.h>
18 #include <common/bitops.h>
19 #include <common/kprint.h>
20 #include <common/macro.h>
21 #include <common/lock.h>
22 #include <arch/sync.h>
23 #ifdef CHCORE_OH_TEE
24 #include <common/tee_uuid.h>
25 #endif
26
27 struct object_slot {
28 int slot_id;
29 struct cap_group *cap_group;
30
31 int isvalid;
32 struct object *object;
33 /* link copied slots pointing to the same object */
34 struct list_head copies;
35 };
36
37 #define BASE_OBJECT_NUM BITS_PER_LONG
38 /* 1st cap is cap_group. 2nd cap is vmspace */
39 #define CAP_GROUP_OBJ_ID 0
40 #define VMSPACE_OBJ_ID 1
41
42 struct slot_table {
43 unsigned int slots_size;
44 struct object_slot **slots;
45 /*
46 * if a bit in full_slots_bmp is 1, corresponding
47 * sizeof(unsigned long) bits in slots_bmp are all set
48 */
49 unsigned long *full_slots_bmp;
50 unsigned long *slots_bmp;
51 /* XXX: Protect mapping of slot_id to slot. Maybe RCU is more suitable */
52 struct rwlock table_guard;
53 };
54
55 #define MAX_GROUP_NAME_LEN 63
56
57 struct cap_group {
58 struct slot_table slot_table;
59
60 /* Proctect thread_list and thread_cnt */
61 struct lock threads_lock;
62 struct list_head thread_list;
63 /* The number of threads */
64 int thread_cnt;
65
66 /*
67 * Each process has a unique badge as a global identifier which
68 * is set by the system server, procmgr.
69 * Currently, badge is used as a client ID during IPC.
70 */
71 badge_t badge;
72
73 /* Ensures the cap_group_exit function only be executed once */
74 int notify_recycler;
75
76 /* Now is used for debugging */
77 char cap_group_name[MAX_GROUP_NAME_LEN + 1];
78
79 #ifdef CHCORE_OH_TEE
80 /* Used in OH-TEE */
81 int pid;
82 TEE_UUID uuid;
83 struct lock heap_size_lock;
84 size_t heap_size_limit;
85 size_t heap_size_used;
86 #endif /* CHCORE_OH_TEE */
87 };
88
89 #define current_cap_group (current_thread->cap_group)
90
91 /*
92 * ATTENTION: These interfaces are for capability internal use.
93 * As a cap user, check object.h for interfaces for cap.
94 */
95 int alloc_slot_id(struct cap_group *cap_group);
96
free_slot_id(struct cap_group * cap_group,cap_t slot_id)97 static inline void free_slot_id(struct cap_group *cap_group, cap_t slot_id)
98 {
99 struct slot_table *slot_table = &cap_group->slot_table;
100 clear_bit(slot_id, slot_table->slots_bmp);
101 clear_bit(slot_id / BITS_PER_LONG, slot_table->full_slots_bmp);
102 slot_table->slots[slot_id] = NULL;
103 }
104
get_slot(struct cap_group * cap_group,cap_t slot_id)105 static inline struct object_slot *get_slot(struct cap_group *cap_group,
106 cap_t slot_id)
107 {
108 if (slot_id < 0 || slot_id >= cap_group->slot_table.slots_size)
109 return NULL;
110 return cap_group->slot_table.slots[slot_id];
111 }
112
install_slot(struct cap_group * cap_group,cap_t slot_id,struct object_slot * slot)113 static inline void install_slot(struct cap_group *cap_group, cap_t slot_id,
114 struct object_slot *slot)
115 {
116 BUG_ON(!get_bit(slot_id, cap_group->slot_table.slots_bmp));
117 cap_group->slot_table.slots[slot_id] = slot;
118 }
119
120 void *get_opaque(struct cap_group *cap_group, cap_t slot_id, bool type_valid,
121 int type);
122
123 int __cap_free(struct cap_group *cap_group, cap_t slot_id,
124 bool slot_table_locked, bool copies_list_locked);
125
126 struct cap_group *create_root_cap_group(char *, size_t);
127
128 void cap_group_deinit(void *ptr);
129
130 /* Fixed badge for root process and servers */
131 #define ROOT_CAP_GROUP_BADGE (1) /* INIT */
132 #define PROCMGR_BADGE ROOT_CAP_GROUP_BADGE
133 #define FSM_BADGE (2)
134 #define LWIP_BADGE (3)
135 #define TMPFS_BADGE (4)
136 #define SERVER_BADGE_START (5)
137 #define DRIVER_BADGE_START (100)
138 #define APP_BADGE_START (200)
139
140 /**
141 * Fixed pcid for root process (PROCMGR_PCID) and servers,
142 * which is exacly the same to the definition in proc.h.
143 */
144 #define ROOT_PROCESS_PCID (1)
145 #define FSM_PCID (2)
146 #define LWIP_PCID (3)
147 #define TMPFS_PCID (4)
148
149 /* Syscalls */
150 cap_t sys_create_cap_group(unsigned long cap_group_args_p);
151
152 #endif /* OBJECT_CAP_GROUP_H */
153