1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef _LOS_PROCESS_PRI_H
33 #define _LOS_PROCESS_PRI_H
34
35 #include "los_task_pri.h"
36 #include "los_sem_pri.h"
37 #include "los_process.h"
38 #include "los_vm_map.h"
39 #ifdef LOSCFG_KERNEL_LITEIPC
40 #include "hm_liteipc.h"
41 #endif
42 #ifdef LOSCFG_SECURITY_CAPABILITY
43 #include "capability_type.h"
44 #endif
45 #ifdef LOSCFG_SECURITY_VID
46 #include "vid_type.h"
47 #endif
48 #include "sys/resource.h"
49
50 #ifdef __cplusplus
51 #if __cplusplus
52 extern "C" {
53 #endif /* __cplusplus */
54 #endif /* __cplusplus */
55
56 #define OS_PCB_NAME_LEN OS_TCB_NAME_LEN
57
58 #ifdef LOSCFG_SECURITY_CAPABILITY
59 #define OS_GROUPS_NUMBER_MAX 256
60
61 typedef struct {
62 UINT32 userID;
63 UINT32 effUserID;
64 UINT32 gid;
65 UINT32 effGid;
66 UINT32 groupNumber;
67 UINT32 groups[1];
68 } User;
69 #endif
70
71 typedef struct {
72 UINT32 groupID; /**< Process group ID is the PID of the process that created the group */
73 LOS_DL_LIST processList; /**< List of processes under this process group */
74 LOS_DL_LIST exitProcessList; /**< List of closed processes (zombie processes) under this group */
75 LOS_DL_LIST groupList; /**< Process group list */
76 } ProcessGroup;
77
78 typedef struct ProcessCB {
79 CHAR processName[OS_PCB_NAME_LEN]; /**< Process name */
80 UINT32 processID; /**< Process ID */
81 UINT16 processStatus; /**< [15:4] Process Status; [3:0] The number of threads currently
82 running in the process */
83 UINT16 consoleID; /**< The console id of task belongs */
84 UINT16 processMode; /**< Kernel Mode:0; User Mode:1; */
85 UINT32 parentProcessID; /**< Parent process ID */
86 UINT32 exitCode; /**< Process exit status */
87 LOS_DL_LIST pendList; /**< Block list to which the process belongs */
88 LOS_DL_LIST childrenList; /**< Children process list */
89 LOS_DL_LIST exitChildList; /**< Exit children process list */
90 LOS_DL_LIST siblingList; /**< Linkage in parent's children list */
91 ProcessGroup *group; /**< Process group to which a process belongs */
92 LOS_DL_LIST subordinateGroupList; /**< Linkage in group list */
93 UINT32 threadGroupID; /**< Which thread group , is the main thread ID of the process */
94 LOS_DL_LIST threadSiblingList; /**< List of threads under this process */
95 volatile UINT32 threadNumber; /**< Number of threads alive under this process */
96 UINT32 threadCount; /**< Total number of threads created under this process */
97 LOS_DL_LIST waitList; /**< The process holds the waitLits to support wait/waitpid */
98 #ifdef LOSCFG_KERNEL_SMP
99 UINT32 timerCpu; /**< CPU core number of this task is delayed or pended */
100 #endif
101 UINTPTR sigHandler; /**< Signal handler */
102 sigset_t sigShare; /**< Signal share bit */
103 #ifdef LOSCFG_KERNEL_LITEIPC
104 ProcIpcInfo *ipcInfo; /**< Memory pool for lite ipc */
105 #endif
106 #ifdef LOSCFG_KERNEL_VM
107 LosVmSpace *vmSpace; /**< VMM space for processes */
108 #endif
109 #ifdef LOSCFG_FS_VFS
110 struct files_struct *files; /**< Files held by the process */
111 #endif
112 timer_t timerID; /**< ITimer */
113
114 #ifdef LOSCFG_SECURITY_CAPABILITY
115 User *user;
116 UINT32 capability;
117 #endif
118 #ifdef LOSCFG_SECURITY_VID
119 TimerIdMap timerIdMap;
120 #endif
121 #ifdef LOSCFG_DRIVERS_TZDRIVER
122 struct Vnode *execVnode; /**< Exec bin of the process */
123 #endif
124 mode_t umask;
125 #ifdef LOSCFG_KERNEL_CPUP
126 OsCpupBase *processCpup; /**< Process cpu usage */
127 #endif
128 struct rlimit *resourceLimit;
129 } LosProcessCB;
130
131 #define CLONE_VM 0x00000100
132 #define CLONE_FS 0x00000200
133 #define CLONE_FILES 0x00000400
134 #define CLONE_SIGHAND 0x00000800
135 #define CLONE_PTRACE 0x00002000
136 #define CLONE_VFORK 0x00004000
137 #define CLONE_PARENT 0x00008000
138 #define CLONE_THREAD 0x00010000
139
140 #define OS_PCB_FROM_PID(processID) (((LosProcessCB *)g_processCBArray) + (processID))
141 #define OS_PCB_FROM_SIBLIST(ptr) LOS_DL_LIST_ENTRY((ptr), LosProcessCB, siblingList)
142 #define OS_PCB_FROM_PENDLIST(ptr) LOS_DL_LIST_ENTRY((ptr), LosProcessCB, pendList)
143
144 /**
145 * @ingroup los_process
146 * Flag that indicates the process or process control block status.
147 *
148 * The process is created but does not participate in scheduling.
149 */
150 #define OS_PROCESS_STATUS_INIT OS_TASK_STATUS_INIT
151
152 /**
153 * @ingroup los_process
154 * Flag that indicates the process or process control block status.
155 *
156 * The process is ready.
157 */
158 #define OS_PROCESS_STATUS_READY OS_TASK_STATUS_READY
159
160 /**
161 * @ingroup los_process
162 * Flag that indicates the process or process control block status.
163 *
164 * The process is running.
165 */
166 #define OS_PROCESS_STATUS_RUNNING OS_TASK_STATUS_RUNNING
167
168 /**
169 * @ingroup los_process
170 * Flag that indicates the process or process control block status.
171 *
172 * The process is pending
173 */
174 #define OS_PROCESS_STATUS_PENDING (OS_TASK_STATUS_PENDING | OS_TASK_STATUS_DELAY | OS_TASK_STATUS_SUSPENDED)
175
176 /**
177 * @ingroup los_process
178 * Flag that indicates the process or process control block status.
179 *
180 * The process is run out but the resources occupied by the process are not recovered.
181 */
182 #define OS_PROCESS_STATUS_ZOMBIES 0x0100U
183
184 /**
185 * @ingroup los_process
186 * Flag that indicates the process or process control block status.
187 *
188 * The process status equal this is process control block unused,
189 * coexisting with OS_PROCESS_STATUS_ZOMBIES means that the control block is not recovered.
190 */
191 #define OS_PROCESS_FLAG_UNUSED 0x0200U
192
193 /**
194 * @ingroup los_process
195 * Flag that indicates the process or process control block status.
196 *
197 * The process has been call exit, it only works with multiple cores.
198 */
199 #define OS_PROCESS_FLAG_EXIT 0x0400U
200
201 /**
202 * @ingroup los_process
203 * Flag that indicates the process or process control block status.
204 *
205 * The process is the leader of the process group.
206 */
207 #define OS_PROCESS_FLAG_GROUP_LEADER 0x0800U
208
209 /**
210 * @ingroup los_process
211 * Flag that indicates the process or process control block status.
212 *
213 * The process has performed the exec operation.
214 */
215 #define OS_PROCESS_FLAG_ALREADY_EXEC 0x1000U
216
217 /**
218 * @ingroup los_process
219 * Flag that indicates the process or process control block status.
220 *
221 * The process is dying or already dying.
222 */
223 #define OS_PROCESS_STATUS_INACTIVE (OS_PROCESS_FLAG_EXIT | OS_PROCESS_STATUS_ZOMBIES)
224
225 /**
226 * @ingroup los_process
227 * Used to check if the process control block is unused.
228 */
OsProcessIsUnused(const LosProcessCB * processCB)229 STATIC INLINE BOOL OsProcessIsUnused(const LosProcessCB *processCB)
230 {
231 return ((processCB->processStatus & OS_PROCESS_FLAG_UNUSED) != 0);
232 }
233
234 /**
235 * @ingroup los_process
236 * Used to check if the process is inactive.
237 */
OsProcessIsInactive(const LosProcessCB * processCB)238 STATIC INLINE BOOL OsProcessIsInactive(const LosProcessCB *processCB)
239 {
240 return ((processCB->processStatus & (OS_PROCESS_FLAG_UNUSED | OS_PROCESS_STATUS_INACTIVE)) != 0);
241 }
242
243 /**
244 * @ingroup los_process
245 * Used to check if the process is dead.
246 */
OsProcessIsDead(const LosProcessCB * processCB)247 STATIC INLINE BOOL OsProcessIsDead(const LosProcessCB *processCB)
248 {
249 return ((processCB->processStatus & (OS_PROCESS_FLAG_UNUSED | OS_PROCESS_STATUS_ZOMBIES)) != 0);
250 }
251
OsProcessIsInit(const LosProcessCB * processCB)252 STATIC INLINE BOOL OsProcessIsInit(const LosProcessCB *processCB)
253 {
254 return (processCB->processStatus & OS_PROCESS_STATUS_INIT);
255 }
256
257 /**
258 * @ingroup los_process
259 * The highest priority of a kernel mode process.
260 */
261 #define OS_PROCESS_PRIORITY_HIGHEST 0
262
263 /**
264 * @ingroup los_process
265 * The lowest priority of a kernel mode process
266 */
267 #define OS_PROCESS_PRIORITY_LOWEST 31
268
269 /**
270 * @ingroup los_process
271 * The highest priority of a user mode process.
272 */
273 #define OS_USER_PROCESS_PRIORITY_HIGHEST 10
274
275 /**
276 * @ingroup los_process
277 * The lowest priority of a user mode process
278 */
279 #define OS_USER_PROCESS_PRIORITY_LOWEST OS_PROCESS_PRIORITY_LOWEST
280
281 /**
282 * @ingroup los_process
283 * User state root process default priority
284 */
285 #define OS_PROCESS_USERINIT_PRIORITY 28
286
287 #define OS_TASK_DEFAULT_STACK_SIZE 0x2000
288 #define OS_USER_TASK_SYSCALL_STACK_SIZE 0x3000
289 #define OS_USER_TASK_STACK_SIZE 0x100000
290
291 #define OS_KERNEL_MODE 0x0U
292 #define OS_USER_MODE 0x1U
OsProcessIsUserMode(const LosProcessCB * processCB)293 STATIC INLINE BOOL OsProcessIsUserMode(const LosProcessCB *processCB)
294 {
295 return (processCB->processMode == OS_USER_MODE);
296 }
297
298 #define LOS_PRIO_PROCESS 0U
299 #define LOS_PRIO_PGRP 1U
300 #define LOS_PRIO_USER 2U
301
302 #define OS_USER_PRIVILEGE_PROCESS_GROUP 1U
303 #define OS_KERNEL_PROCESS_GROUP 2U
304
305 /*
306 * Process exit code
307 * 31 15 8 7 0
308 * | | exit code | core dump | signal |
309 */
310 #define OS_PRO_EXIT_OK 0
311
OsProcessExitCodeCoreDumpSet(LosProcessCB * processCB)312 STATIC INLINE VOID OsProcessExitCodeCoreDumpSet(LosProcessCB *processCB)
313 {
314 processCB->exitCode |= 0x80U;
315 }
316
OsProcessExitCodeSignalSet(LosProcessCB * processCB,UINT32 signal)317 STATIC INLINE VOID OsProcessExitCodeSignalSet(LosProcessCB *processCB, UINT32 signal)
318 {
319 processCB->exitCode |= signal & 0x7FU;
320 }
321
OsProcessExitCodeSignalClear(LosProcessCB * processCB)322 STATIC INLINE VOID OsProcessExitCodeSignalClear(LosProcessCB *processCB)
323 {
324 processCB->exitCode &= (~0x7FU);
325 }
326
OsProcessExitCodeSignalIsSet(LosProcessCB * processCB)327 STATIC INLINE BOOL OsProcessExitCodeSignalIsSet(LosProcessCB *processCB)
328 {
329 return (processCB->exitCode) & 0x7FU;
330 }
331
OsProcessExitCodeSet(LosProcessCB * processCB,UINT32 code)332 STATIC INLINE VOID OsProcessExitCodeSet(LosProcessCB *processCB, UINT32 code)
333 {
334 processCB->exitCode |= ((code & 0x000000FFU) << 8U) & 0x0000FF00U; /* 8: Move 8 bits to the left, exitCode */
335 }
336
337 extern LosProcessCB *g_processCBArray;
338 extern UINT32 g_processMaxNum;
339
340 #define OS_PID_CHECK_INVALID(pid) (((UINT32)(pid)) >= g_processMaxNum)
341
OsProcessIDUserCheckInvalid(UINT32 pid)342 STATIC INLINE BOOL OsProcessIDUserCheckInvalid(UINT32 pid)
343 {
344 return ((pid >= g_processMaxNum) || (pid == 0));
345 }
346
OsCurrProcessGet(VOID)347 STATIC INLINE LosProcessCB *OsCurrProcessGet(VOID)
348 {
349 UINT32 intSave;
350
351 intSave = LOS_IntLock();
352 LosProcessCB *runProcess = OS_PCB_FROM_PID(OsCurrTaskGet()->processID);
353 LOS_IntRestore(intSave);
354 return runProcess;
355 }
356
357 #ifdef LOSCFG_SECURITY_CAPABILITY
OsCurrUserGet(VOID)358 STATIC INLINE User *OsCurrUserGet(VOID)
359 {
360 User *user = NULL;
361 UINT32 intSave;
362
363 intSave = LOS_IntLock();
364 user = OsCurrProcessGet()->user;
365 LOS_IntRestore(intSave);
366 return user;
367 }
368
OsProcessUserIDGet(const LosTaskCB * taskCB)369 STATIC INLINE UINT32 OsProcessUserIDGet(const LosTaskCB *taskCB)
370 {
371 UINT32 intSave = LOS_IntLock();
372 UINT32 uid = OS_INVALID;
373
374 LosProcessCB *process = OS_PCB_FROM_PID(taskCB->processID);
375 if (process->user != NULL) {
376 uid = process->user->userID;
377 }
378 LOS_IntRestore(intSave);
379 return uid;
380 }
381 #endif
382
OsProcessThreadGroupIDGet(const LosTaskCB * taskCB)383 STATIC INLINE UINT32 OsProcessThreadGroupIDGet(const LosTaskCB *taskCB)
384 {
385 return OS_PCB_FROM_PID(taskCB->processID)->threadGroupID;
386 }
387
OsProcessThreadNumberGet(const LosTaskCB * taskCB)388 STATIC INLINE UINT32 OsProcessThreadNumberGet(const LosTaskCB *taskCB)
389 {
390 return OS_PCB_FROM_PID(taskCB->processID)->threadNumber;
391 }
392
393 #ifdef LOSCFG_KERNEL_VM
OsProcessVmSpaceGet(const LosProcessCB * processCB)394 STATIC INLINE LosVmSpace *OsProcessVmSpaceGet(const LosProcessCB *processCB)
395 {
396 return processCB->vmSpace;
397 }
398 #endif
399
400 #ifdef LOSCFG_DRIVERS_TZDRIVER
OsProcessExecVnodeGet(const LosProcessCB * processCB)401 STATIC INLINE struct Vnode *OsProcessExecVnodeGet(const LosProcessCB *processCB)
402 {
403 return processCB->execVnode;
404 }
405 #endif
406 /*
407 * return immediately if no child has exited.
408 */
409 #define LOS_WAIT_WNOHANG (1 << 0U)
410
411 /*
412 * return if a child has stopped (but not traced via ptrace(2)).
413 * Status for traced children which have stopped is provided even
414 * if this option is not specified.
415 */
416 #define LOS_WAIT_WUNTRACED (1 << 1U)
417 #define LOS_WAIT_WSTOPPED (1 << 1U)
418
419 /*
420 * Wait for exited processes
421 */
422 #define LOS_WAIT_WEXITED (1 << 2U)
423
424 /*
425 * return if a stopped child has been resumed by delivery of SIGCONT.
426 * (For Linux-only options, see below.)
427 */
428 #define LOS_WAIT_WCONTINUED (1 << 3U)
429
430 /*
431 * Leave the child in a waitable state;
432 * a later wait call can be used to again retrieve the child status information.
433 */
434 #define LOS_WAIT_WNOWAIT (1 << 24U)
435
436 /*
437 * Indicates that you are already in a wait state
438 */
439 #define OS_PROCESS_WAIT (1 << 15U)
440
441 /*
442 * Wait for any child process to finish
443 */
444 #define OS_PROCESS_WAIT_ANY OS_TASK_WAIT_ANYPROCESS
445
446 /*
447 * Wait for the child process specified by the pid to finish
448 */
449 #define OS_PROCESS_WAIT_PRO OS_TASK_WAIT_PROCESS
450
451 /*
452 * Waits for any child process in the specified process group to finish.
453 */
454 #define OS_PROCESS_WAIT_GID OS_TASK_WAIT_GID
455
456 #define OS_PROCESS_INFO_ALL 1
457 #define OS_PROCESS_DEFAULT_UMASK 0022
458
459 extern UINTPTR __user_init_entry;
460 extern UINTPTR __user_init_bss;
461 extern UINTPTR __user_init_end;
462 extern UINTPTR __user_init_load_addr;
463 extern UINT32 OsSystemProcessCreate(VOID);
464 extern VOID OsProcessNaturalExit(LosProcessCB *processCB, UINT32 status);
465 extern VOID OsProcessCBRecycleToFree(VOID);
466 extern VOID OsProcessResourcesToFree(LosProcessCB *processCB);
467 extern UINT32 OsUserInitProcess(VOID);
468 extern INT32 OsClone(UINT32 flags, UINTPTR sp, UINT32 size);
469 extern VOID OsExecProcessVmSpaceRestore(LosVmSpace *oldSpace);
470 extern LosVmSpace *OsExecProcessVmSpaceReplace(LosVmSpace *newSpace, UINTPTR stackBase, INT32 randomDevFD);
471 extern UINT32 OsExecRecycleAndInit(LosProcessCB *processCB, const CHAR *name, LosVmSpace *oldAspace, UINTPTR oldFiles);
472 extern UINT32 OsExecStart(const TSK_ENTRY_FUNC entry, UINTPTR sp, UINTPTR mapBase, UINT32 mapSize);
473 extern UINT32 OsSetProcessName(LosProcessCB *processCB, const CHAR *name);
474 extern INT32 OsSetProcessScheduler(INT32 which, INT32 pid, UINT16 prio, UINT16 policy);
475 extern INT32 OsGetProcessPriority(INT32 which, INT32 pid);
476 extern UINT32 OsGetUserInitProcessID(VOID);
477 extern UINT32 OsGetIdleProcessID(VOID);
478 extern INT32 OsSetProcessGroupID(UINT32 pid, UINT32 gid);
479 extern INT32 OsSetCurrProcessGroupID(UINT32 gid);
480 extern UINT32 OsGetKernelInitProcessID(VOID);
481 extern VOID OsSetSigHandler(UINTPTR addr);
482 extern UINTPTR OsGetSigHandler(VOID);
483 extern VOID OsWaitWakeTask(LosTaskCB *taskCB, UINT32 wakePID);
484 extern INT32 OsSendSignalToProcessGroup(INT32 pid, siginfo_t *info, INT32 permission);
485 extern INT32 OsSendSignalToAllProcess(siginfo_t *info, INT32 permission);
486 extern UINT32 OsProcessAddNewTask(UINT32 pid, LosTaskCB *taskCB, SchedParam *param);
487 extern VOID OsDeleteTaskFromProcess(LosTaskCB *taskCB);
488 extern VOID OsProcessThreadGroupDestroy(VOID);
489
490 #ifdef __cplusplus
491 #if __cplusplus
492 }
493 #endif /* __cplusplus */
494 #endif /* __cplusplus */
495
496 #endif
497