• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 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_SCHED_NORMAL  0U
299 #define LOS_SCHED_FIFO    1U
300 #define LOS_SCHED_RR      2U
301 #define LOS_SCHED_IDLE    3U
302 
303 #define LOS_PRIO_PROCESS  0U
304 #define LOS_PRIO_PGRP     1U
305 #define LOS_PRIO_USER     2U
306 
307 #define OS_USER_PRIVILEGE_PROCESS_GROUP 1U
308 #define OS_KERNEL_PROCESS_GROUP         2U
309 
310 /*
311  * Process exit code
312  * 31    15           8           7        0
313  * |     | exit code  | core dump | signal |
314  */
315 #define OS_PRO_EXIT_OK 0
316 
OsProcessExitCodeCoreDumpSet(LosProcessCB * processCB)317 STATIC INLINE VOID OsProcessExitCodeCoreDumpSet(LosProcessCB *processCB)
318 {
319     processCB->exitCode |= 0x80U;
320 }
321 
OsProcessExitCodeSignalSet(LosProcessCB * processCB,UINT32 signal)322 STATIC INLINE VOID OsProcessExitCodeSignalSet(LosProcessCB *processCB, UINT32 signal)
323 {
324     processCB->exitCode |= signal & 0x7FU;
325 }
326 
OsProcessExitCodeSignalClear(LosProcessCB * processCB)327 STATIC INLINE VOID OsProcessExitCodeSignalClear(LosProcessCB *processCB)
328 {
329     processCB->exitCode &= (~0x7FU);
330 }
331 
OsProcessExitCodeSignalIsSet(LosProcessCB * processCB)332 STATIC INLINE BOOL OsProcessExitCodeSignalIsSet(LosProcessCB *processCB)
333 {
334     return (processCB->exitCode) & 0x7FU;
335 }
336 
OsProcessExitCodeSet(LosProcessCB * processCB,UINT32 code)337 STATIC INLINE VOID OsProcessExitCodeSet(LosProcessCB *processCB, UINT32 code)
338 {
339     processCB->exitCode |= ((code & 0x000000FFU) << 8U) & 0x0000FF00U; /* 8: Move 8 bits to the left, exitCode */
340 }
341 
342 extern LosProcessCB *g_processCBArray;
343 extern UINT32 g_processMaxNum;
344 
345 #define OS_PID_CHECK_INVALID(pid) (((UINT32)(pid)) >= g_processMaxNum)
346 
OsProcessIDUserCheckInvalid(UINT32 pid)347 STATIC INLINE BOOL OsProcessIDUserCheckInvalid(UINT32 pid)
348 {
349     return ((pid >= g_processMaxNum) || (pid == 0));
350 }
351 
OsCurrProcessGet(VOID)352 STATIC INLINE LosProcessCB *OsCurrProcessGet(VOID)
353 {
354     UINT32 intSave;
355 
356     intSave = LOS_IntLock();
357     LosProcessCB *runProcess = OS_PCB_FROM_PID(OsCurrTaskGet()->processID);
358     LOS_IntRestore(intSave);
359     return runProcess;
360 }
361 
362 #ifdef LOSCFG_SECURITY_CAPABILITY
OsCurrUserGet(VOID)363 STATIC INLINE User *OsCurrUserGet(VOID)
364 {
365     User *user = NULL;
366     UINT32 intSave;
367 
368     intSave = LOS_IntLock();
369     user = OsCurrProcessGet()->user;
370     LOS_IntRestore(intSave);
371     return user;
372 }
373 
OsProcessUserIDGet(const LosTaskCB * taskCB)374 STATIC INLINE UINT32 OsProcessUserIDGet(const LosTaskCB *taskCB)
375 {
376     UINT32 intSave = LOS_IntLock();
377     UINT32 uid = OS_INVALID;
378 
379     LosProcessCB *process = OS_PCB_FROM_PID(taskCB->processID);
380     if (process->user != NULL) {
381         uid = process->user->userID;
382     }
383     LOS_IntRestore(intSave);
384     return uid;
385 }
386 #endif
387 
OsProcessThreadGroupIDGet(const LosTaskCB * taskCB)388 STATIC INLINE UINT32 OsProcessThreadGroupIDGet(const LosTaskCB *taskCB)
389 {
390     return OS_PCB_FROM_PID(taskCB->processID)->threadGroupID;
391 }
392 
OsProcessThreadNumberGet(const LosTaskCB * taskCB)393 STATIC INLINE UINT32 OsProcessThreadNumberGet(const LosTaskCB *taskCB)
394 {
395     return OS_PCB_FROM_PID(taskCB->processID)->threadNumber;
396 }
397 
398 #ifdef LOSCFG_KERNEL_VM
OsProcessVmSpaceGet(const LosProcessCB * processCB)399 STATIC INLINE LosVmSpace *OsProcessVmSpaceGet(const LosProcessCB *processCB)
400 {
401     return processCB->vmSpace;
402 }
403 #endif
404 
405 #ifdef LOSCFG_DRIVERS_TZDRIVER
OsProcessExecVnodeGet(const LosProcessCB * processCB)406 STATIC INLINE struct Vnode *OsProcessExecVnodeGet(const LosProcessCB *processCB)
407 {
408     return processCB->execVnode;
409 }
410 #endif
411 /*
412  * return immediately if no child has exited.
413  */
414 #define LOS_WAIT_WNOHANG   (1 << 0U)
415 
416 /*
417  * return if a child has stopped (but not traced via ptrace(2)).
418  * Status for traced children which have stopped is provided even
419  * if this option is not specified.
420  */
421 #define LOS_WAIT_WUNTRACED (1 << 1U)
422 #define LOS_WAIT_WSTOPPED (1 << 1U)
423 
424 /*
425  * Wait for exited processes
426  */
427 #define LOS_WAIT_WEXITED (1 << 2U)
428 
429 /*
430  * return if a stopped child has been resumed by delivery of SIGCONT.
431  * (For Linux-only options, see below.)
432  */
433 #define LOS_WAIT_WCONTINUED (1 << 3U)
434 
435 /*
436  * Leave the child in a waitable state;
437  * a later wait call can be used to again retrieve the child status information.
438  */
439 #define LOS_WAIT_WNOWAIT (1 << 24U)
440 
441 /*
442  * Indicates that you are already in a wait state
443  */
444 #define OS_PROCESS_WAIT     (1 << 15U)
445 
446 /*
447  * Wait for any child process to finish
448  */
449 #define OS_PROCESS_WAIT_ANY OS_TASK_WAIT_ANYPROCESS
450 
451 /*
452  * Wait for the child process specified by the pid to finish
453  */
454 #define OS_PROCESS_WAIT_PRO OS_TASK_WAIT_PROCESS
455 
456 /*
457  * Waits for any child process in the specified process group to finish.
458  */
459 #define OS_PROCESS_WAIT_GID OS_TASK_WAIT_GID
460 
461 #define OS_PROCESS_INFO_ALL 1
462 #define OS_PROCESS_DEFAULT_UMASK 0022
463 
464 extern UINTPTR __user_init_entry;
465 extern UINTPTR __user_init_bss;
466 extern UINTPTR __user_init_end;
467 extern UINTPTR __user_init_load_addr;
468 extern UINT32 OsSystemProcessCreate(VOID);
469 extern VOID OsProcessNaturalExit(LosProcessCB *processCB, UINT32 status);
470 extern VOID OsProcessCBRecycleToFree(VOID);
471 extern VOID OsProcessResourcesToFree(LosProcessCB *processCB);
472 extern UINT32 OsUserInitProcess(VOID);
473 extern INT32 OsClone(UINT32 flags, UINTPTR sp, UINT32 size);
474 extern VOID OsExecProcessVmSpaceRestore(LosVmSpace *oldSpace);
475 extern LosVmSpace *OsExecProcessVmSpaceReplace(LosVmSpace *newSpace, UINTPTR stackBase, INT32 randomDevFD);
476 extern UINT32 OsExecRecycleAndInit(LosProcessCB *processCB, const CHAR *name, LosVmSpace *oldAspace, UINTPTR oldFiles);
477 extern UINT32 OsExecStart(const TSK_ENTRY_FUNC entry, UINTPTR sp, UINTPTR mapBase, UINT32 mapSize);
478 extern UINT32 OsSetProcessName(LosProcessCB *processCB, const CHAR *name);
479 extern INT32 OsSetProcessScheduler(INT32 which, INT32 pid, UINT16 prio, UINT16 policy);
480 extern INT32 OsGetProcessPriority(INT32 which, INT32 pid);
481 extern UINT32 OsGetUserInitProcessID(VOID);
482 extern UINT32 OsGetIdleProcessID(VOID);
483 extern INT32 OsSetProcessGroupID(UINT32 pid, UINT32 gid);
484 extern INT32 OsSetCurrProcessGroupID(UINT32 gid);
485 extern UINT32 OsGetKernelInitProcessID(VOID);
486 extern VOID OsSetSigHandler(UINTPTR addr);
487 extern UINTPTR OsGetSigHandler(VOID);
488 extern VOID OsWaitWakeTask(LosTaskCB *taskCB, UINT32 wakePID);
489 extern INT32 OsSendSignalToProcessGroup(INT32 pid, siginfo_t *info, INT32 permission);
490 extern INT32 OsSendSignalToAllProcess(siginfo_t *info, INT32 permission);
491 extern UINT32 OsProcessAddNewTask(UINT32 pid, LosTaskCB *taskCB);
492 extern VOID OsDeleteTaskFromProcess(LosTaskCB *taskCB);
493 extern VOID OsProcessThreadGroupDestroy(VOID);
494 
495 #ifdef __cplusplus
496 #if __cplusplus
497 }
498 #endif /* __cplusplus */
499 #endif /* __cplusplus */
500 
501 #endif
502