1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2023 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 #include "hm_liteipc.h"
33 #include "linux/kernel.h"
34 #include "fs/file.h"
35 #include "fs/driver.h"
36 #include "los_init.h"
37 #include "los_mp.h"
38 #include "los_mux.h"
39 #include "los_process_pri.h"
40 #include "los_sched_pri.h"
41 #include "los_spinlock.h"
42 #include "los_task_pri.h"
43 #include "los_vm_lock.h"
44 #include "los_vm_map.h"
45 #include "los_vm_page.h"
46 #include "los_vm_phys.h"
47 #include "los_hook.h"
48
49 #define USE_TASKID_AS_HANDLE 1
50 #define USE_MMAP 1
51 #define IPC_IO_DATA_MAX 8192UL
52 #define IPC_MSG_DATA_SZ_MAX (IPC_IO_DATA_MAX * sizeof(SpecialObj) / (sizeof(SpecialObj) + sizeof(size_t)))
53 #define IPC_MSG_OBJECT_NUM_MAX (IPC_MSG_DATA_SZ_MAX / sizeof(SpecialObj))
54
55 #define LITE_IPC_POOL_NAME "liteipc"
56 #define LITE_IPC_POOL_PAGE_MAX_NUM 64 /* 256KB */
57 #define LITE_IPC_POOL_PAGE_DEFAULT_NUM 16 /* 64KB */
58 #define LITE_IPC_POOL_MAX_SIZE (LITE_IPC_POOL_PAGE_MAX_NUM << PAGE_SHIFT)
59 #define LITE_IPC_POOL_DEFAULT_SIZE (LITE_IPC_POOL_PAGE_DEFAULT_NUM << PAGE_SHIFT)
60 #define LITE_IPC_POOL_UVADDR 0x10000000
61 #define INVAILD_ID (-1)
62
63 #define LITEIPC_TIMEOUT_MS 5000UL
64 #define LITEIPC_TIMEOUT_NS 5000000000ULL
65
66 #define MAJOR_VERSION (2)
67 #define MINOR_VERSION (0)
68 #define DRIVER_VERSION (MAJOR_VERSION | MINOR_VERSION << 16)
69
70 typedef struct {
71 LOS_DL_LIST list;
72 VOID *ptr;
73 } IpcUsedNode;
74
75 STATIC LosMux g_serviceHandleMapMux;
76 #if (USE_TASKID_AS_HANDLE == 1)
77 STATIC HandleInfo g_cmsTask;
78 #else
79 STATIC HandleInfo g_serviceHandleMap[MAX_SERVICE_NUM];
80 #endif
81 STATIC LOS_DL_LIST g_ipcPendlist;
82
83 /* ipc lock */
84 SPIN_LOCK_INIT(g_ipcSpin);
85 #define IPC_LOCK(state) LOS_SpinLockSave(&g_ipcSpin, &(state))
86 #define IPC_UNLOCK(state) LOS_SpinUnlockRestore(&g_ipcSpin, state)
87
88 STATIC int LiteIpcOpen(struct file *filep);
89 STATIC int LiteIpcClose(struct file *filep);
90 STATIC int LiteIpcIoctl(struct file *filep, int cmd, unsigned long arg);
91 STATIC int LiteIpcMmap(struct file* filep, LosVmMapRegion *region);
92 STATIC UINT32 LiteIpcWrite(IpcContent *content);
93 STATIC UINT32 GetTid(UINT32 serviceHandle, UINT32 *taskID);
94 STATIC UINT32 HandleSpecialObjects(UINT32 dstTid, IpcListNode *node, BOOL isRollback);
95 STATIC ProcIpcInfo *LiteIpcPoolCreate(VOID);
96
97 STATIC const struct file_operations_vfs g_liteIpcFops = {
98 .open = LiteIpcOpen, /* open */
99 .close = LiteIpcClose, /* close */
100 .ioctl = LiteIpcIoctl, /* ioctl */
101 .mmap = LiteIpcMmap, /* mmap */
102 };
103
OsLiteIpcInitnull104 LITE_OS_SEC_TEXT_INIT UINT32 OsLiteIpcInit(VOID)
105 {
106 UINT32 ret;
107 #if (USE_TASKID_AS_HANDLE == 1)
108 g_cmsTask.status = HANDLE_NOT_USED;
109 #else
110 (void)memset_s(g_serviceHandleMap, sizeof(g_serviceHandleMap), 0, sizeof(g_serviceHandleMap));
111 #endif
112 ret = LOS_MuxInit(&g_serviceHandleMapMux, NULL);
113 if (ret != LOS_OK) {
114 return ret;
115 }
116 ret = (UINT32)register_driver(LITEIPC_DRIVER, &g_liteIpcFops, LITEIPC_DRIVER_MODE, NULL);
117 if (ret != LOS_OK) {
118 PRINT_ERR("register lite_ipc driver failed:%d\n", ret);
119 }
120 LOS_ListInit(&(g_ipcPendlist));
121
122 return ret;
123 }
124
125 LOS_MODULE_INIT(OsLiteIpcInit, LOS_INIT_LEVEL_KMOD_EXTENDED);
126
LiteIpcOpen(struct file *filep)127 LITE_OS_SEC_TEXT STATIC int LiteIpcOpen(struct file *filep)
128 {
129 LosProcessCB *pcb = OsCurrProcessGet();
130 if (pcb->ipcInfo != NULL) {
131 return 0;
132 }
133
134 pcb->ipcInfo = LiteIpcPoolCreate();
135 if (pcb->ipcInfo == NULL) {
136 return -ENOMEM;
137 }
138
139 return 0;
140 }
141
LiteIpcClose(struct file *filep)142 LITE_OS_SEC_TEXT STATIC int LiteIpcClose(struct file *filep)
143 {
144 return 0;
145 }
146
IsPoolMapped(ProcIpcInfo *ipcInfo)147 LITE_OS_SEC_TEXT STATIC BOOL IsPoolMapped(ProcIpcInfo *ipcInfo)
148 {
149 return (ipcInfo->pool.uvaddr != NULL) && (ipcInfo->pool.kvaddr != NULL) &&
150 (ipcInfo->pool.poolSize != 0);
151 }
152
DoIpcMmap(LosProcessCB *pcb, LosVmMapRegion *region)153 LITE_OS_SEC_TEXT STATIC INT32 DoIpcMmap(LosProcessCB *pcb, LosVmMapRegion *region)
154 {
155 UINT32 i;
156 INT32 ret = 0;
157 PADDR_T pa;
158 UINT32 uflags = VM_MAP_REGION_FLAG_PERM_READ | VM_MAP_REGION_FLAG_PERM_USER;
159 LosVmPage *vmPage = NULL;
160 VADDR_T uva = (VADDR_T)(UINTPTR)pcb->ipcInfo->pool.uvaddr;
161 VADDR_T kva = (VADDR_T)(UINTPTR)pcb->ipcInfo->pool.kvaddr;
162
163 (VOID)LOS_MuxAcquire(&pcb->vmSpace->regionMux);
164
165 for (i = 0; i < (region->range.size >> PAGE_SHIFT); i++) {
166 pa = LOS_PaddrQuery((VOID *)(UINTPTR)(kva + (i << PAGE_SHIFT)));
167 if (pa == 0) {
168 PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
169 ret = -EINVAL;
170 break;
171 }
172 vmPage = LOS_VmPageGet(pa);
173 if (vmPage == NULL) {
174 PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
175 ret = -EINVAL;
176 break;
177 }
178 STATUS_T err = LOS_ArchMmuMap(&pcb->vmSpace->archMmu, uva + (i << PAGE_SHIFT), pa, 1, uflags);
179 if (err < 0) {
180 ret = err;
181 PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
182 break;
183 }
184 }
185 /* if any failure happened, rollback */
186 if (i != (region->range.size >> PAGE_SHIFT)) {
187 while (i--) {
188 pa = LOS_PaddrQuery((VOID *)(UINTPTR)(kva + (i << PAGE_SHIFT)));
189 vmPage = LOS_VmPageGet(pa);
190 (VOID)LOS_ArchMmuUnmap(&pcb->vmSpace->archMmu, uva + (i << PAGE_SHIFT), 1);
191 LOS_PhysPageFree(vmPage);
192 }
193 }
194
195 (VOID)LOS_MuxRelease(&pcb->vmSpace->regionMux);
196 return ret;
197 }
198
LiteIpcMmap(struct file *filep, LosVmMapRegion *region)199 LITE_OS_SEC_TEXT STATIC int LiteIpcMmap(struct file *filep, LosVmMapRegion *region)
200 {
201 int ret = 0;
202 LosVmMapRegion *regionTemp = NULL;
203 LosProcessCB *pcb = OsCurrProcessGet();
204 ProcIpcInfo *ipcInfo = pcb->ipcInfo;
205
206 if ((ipcInfo == NULL) || (region == NULL) || (region->range.size > LITE_IPC_POOL_MAX_SIZE) ||
207 (!LOS_IsRegionPermUserReadOnly(region)) || (!LOS_IsRegionFlagPrivateOnly(region))) {
208 ret = -EINVAL;
209 goto ERROR_REGION_OUT;
210 }
211 if (IsPoolMapped(ipcInfo)) {
212 return -EEXIST;
213 }
214 if (ipcInfo->pool.uvaddr != NULL) {
215 regionTemp = LOS_RegionFind(pcb->vmSpace, (VADDR_T)(UINTPTR)ipcInfo->pool.uvaddr);
216 if (regionTemp != NULL) {
217 (VOID)LOS_RegionFree(pcb->vmSpace, regionTemp);
218 }
219 }
220 ipcInfo->pool.uvaddr = (VOID *)(UINTPTR)region->range.base;
221 if (ipcInfo->pool.kvaddr != NULL) {
222 LOS_VFree(ipcInfo->pool.kvaddr);
223 ipcInfo->pool.kvaddr = NULL;
224 }
225 /* use vmalloc to alloc phy mem */
226 ipcInfo->pool.kvaddr = LOS_VMalloc(region->range.size);
227 if (ipcInfo->pool.kvaddr == NULL) {
228 ret = -ENOMEM;
229 goto ERROR_REGION_OUT;
230 }
231 /* do mmap */
232 ret = DoIpcMmap(pcb, region);
233 if (ret) {
234 goto ERROR_MAP_OUT;
235 }
236 /* ipc pool init */
237 if (LOS_MemInit(ipcInfo->pool.kvaddr, region->range.size) != LOS_OK) {
238 ret = -EINVAL;
239 goto ERROR_MAP_OUT;
240 }
241 ipcInfo->pool.poolSize = region->range.size;
242 region->regionFlags |= VM_MAP_REGION_FLAG_LITEIPC;
243 return 0;
244 ERROR_MAP_OUT:
245 LOS_VFree(ipcInfo->pool.kvaddr);
246 ERROR_REGION_OUT:
247 if (ipcInfo != NULL) {
248 ipcInfo->pool.uvaddr = NULL;
249 ipcInfo->pool.kvaddr = NULL;
250 }
251 return ret;
252 }
253
LiteIpcPoolInit(ProcIpcInfo *ipcInfo)254 LITE_OS_SEC_TEXT_INIT STATIC UINT32 LiteIpcPoolInit(ProcIpcInfo *ipcInfo)
255 {
256 ipcInfo->pool.uvaddr = NULL;
257 ipcInfo->pool.kvaddr = NULL;
258 ipcInfo->pool.poolSize = 0;
259 ipcInfo->ipcTaskID = INVAILD_ID;
260 LOS_ListInit(&ipcInfo->ipcUsedNodelist);
261 return LOS_OK;
262 }
263
LiteIpcPoolCreatenull264 LITE_OS_SEC_TEXT_INIT STATIC ProcIpcInfo *LiteIpcPoolCreate(VOID)
265 {
266 ProcIpcInfo *ipcInfo = LOS_MemAlloc(m_aucSysMem1, sizeof(ProcIpcInfo));
267 if (ipcInfo == NULL) {
268 return NULL;
269 }
270
271 (VOID)memset_s(ipcInfo, sizeof(ProcIpcInfo), 0, sizeof(ProcIpcInfo));
272
273 (VOID)LiteIpcPoolInit(ipcInfo);
274 return ipcInfo;
275 }
276
LiteIpcPoolReInit(const ProcIpcInfo *parent)277 LITE_OS_SEC_TEXT ProcIpcInfo *LiteIpcPoolReInit(const ProcIpcInfo *parent)
278 {
279 ProcIpcInfo *ipcInfo = LiteIpcPoolCreate();
280 if (ipcInfo == NULL) {
281 return NULL;
282 }
283
284 ipcInfo->pool.uvaddr = parent->pool.uvaddr;
285 ipcInfo->pool.kvaddr = NULL;
286 ipcInfo->pool.poolSize = 0;
287 ipcInfo->ipcTaskID = INVAILD_ID;
288 return ipcInfo;
289 }
290
LiteIpcPoolDelete(ProcIpcInfo *ipcInfo, UINT32 processID)291 STATIC VOID LiteIpcPoolDelete(ProcIpcInfo *ipcInfo, UINT32 processID)
292 {
293 UINT32 intSave;
294 IpcUsedNode *node = NULL;
295 if (ipcInfo->pool.kvaddr != NULL) {
296 LOS_VFree(ipcInfo->pool.kvaddr);
297 ipcInfo->pool.kvaddr = NULL;
298 IPC_LOCK(intSave);
299 while (!LOS_ListEmpty(&ipcInfo->ipcUsedNodelist)) {
300 node = LOS_DL_LIST_ENTRY(ipcInfo->ipcUsedNodelist.pstNext, IpcUsedNode, list);
301 LOS_ListDelete(&node->list);
302 free(node);
303 }
304 IPC_UNLOCK(intSave);
305 }
306 /* remove process access to service */
307 for (UINT32 i = 0; i < MAX_SERVICE_NUM; i++) {
308 if (ipcInfo->access[i] == TRUE) {
309 ipcInfo->access[i] = FALSE;
310 if (OS_TCB_FROM_TID(i)->ipcTaskInfo != NULL) {
311 OS_TCB_FROM_TID(i)->ipcTaskInfo->accessMap[processID] = FALSE;
312 }
313 }
314 }
315 }
316
LiteIpcPoolDestroy(UINT32 processID)317 LITE_OS_SEC_TEXT UINT32 LiteIpcPoolDestroy(UINT32 processID)
318 {
319 LosProcessCB *pcb = OS_PCB_FROM_PID(processID);
320 if (pcb->ipcInfo == NULL) {
321 return LOS_NOK;
322 }
323
324 LiteIpcPoolDelete(pcb->ipcInfo, pcb->processID);
325 LOS_MemFree(m_aucSysMem1, pcb->ipcInfo);
326 pcb->ipcInfo = NULL;
327 return LOS_OK;
328 }
329
LiteIpcTaskInitnull330 LITE_OS_SEC_TEXT_INIT STATIC IpcTaskInfo *LiteIpcTaskInit(VOID)
331 {
332 IpcTaskInfo *taskInfo = LOS_MemAlloc((VOID *)m_aucSysMem1, sizeof(IpcTaskInfo));
333 if (taskInfo == NULL) {
334 return NULL;
335 }
336
337 (VOID)memset_s(taskInfo, sizeof(IpcTaskInfo), 0, sizeof(IpcTaskInfo));
338 LOS_ListInit(&taskInfo->msgListHead);
339 return taskInfo;
340 }
341
342 /* Only when kernenl no longer access ipc node content, can user free the ipc node */
EnableIpcNodeFreeByUser(LosProcessCB *pcb, VOID *buf)343 LITE_OS_SEC_TEXT STATIC VOID EnableIpcNodeFreeByUser(LosProcessCB *pcb, VOID *buf)
344 {
345 UINT32 intSave;
346 ProcIpcInfo *ipcInfo = pcb->ipcInfo;
347 IpcUsedNode *node = (IpcUsedNode *)malloc(sizeof(IpcUsedNode));
348 if (node != NULL) {
349 node->ptr = buf;
350 IPC_LOCK(intSave);
351 LOS_ListAdd(&ipcInfo->ipcUsedNodelist, &node->list);
352 IPC_UNLOCK(intSave);
353 }
354 }
355
LiteIpcNodeAlloc(LosProcessCB *pcb, UINT32 size)356 LITE_OS_SEC_TEXT STATIC VOID *LiteIpcNodeAlloc(LosProcessCB *pcb, UINT32 size)
357 {
358 VOID *ptr = LOS_MemAlloc(pcb->ipcInfo->pool.kvaddr, size);
359 PRINT_INFO("LiteIpcNodeAlloc pid:%d, pool:%x buf:%x size:%d\n",
360 pcb->processID, pcb->ipcInfo->pool.kvaddr, ptr, size);
361 return ptr;
362 }
363
LiteIpcNodeFree(LosProcessCB *pcb, VOID *buf)364 LITE_OS_SEC_TEXT STATIC UINT32 LiteIpcNodeFree(LosProcessCB *pcb, VOID *buf)
365 {
366 PRINT_INFO("LiteIpcNodeFree pid:%d, pool:%x buf:%x\n",
367 pcb->processID, pcb->ipcInfo->pool.kvaddr, buf);
368 return LOS_MemFree(pcb->ipcInfo->pool.kvaddr, buf);
369 }
370
IsIpcNode(LosProcessCB *pcb, const VOID *buf)371 LITE_OS_SEC_TEXT STATIC BOOL IsIpcNode(LosProcessCB *pcb, const VOID *buf)
372 {
373 IpcUsedNode *node = NULL;
374 UINT32 intSave;
375 ProcIpcInfo *ipcInfo = pcb->ipcInfo;
376 IPC_LOCK(intSave);
377 LOS_DL_LIST_FOR_EACH_ENTRY(node, &ipcInfo->ipcUsedNodelist, IpcUsedNode, list) {
378 if (node->ptr == buf) {
379 LOS_ListDelete(&node->list);
380 IPC_UNLOCK(intSave);
381 free(node);
382 return TRUE;
383 }
384 }
385 IPC_UNLOCK(intSave);
386 return FALSE;
387 }
388
GetIpcUserAddr(const LosProcessCB *pcb, INTPTR kernelAddr)389 LITE_OS_SEC_TEXT STATIC INTPTR GetIpcUserAddr(const LosProcessCB *pcb, INTPTR kernelAddr)
390 {
391 IpcPool pool = pcb->ipcInfo->pool;
392 INTPTR offset = (INTPTR)(pool.uvaddr) - (INTPTR)(pool.kvaddr);
393 return kernelAddr + offset;
394 }
395
GetIpcKernelAddr(const LosProcessCB *pcb, INTPTR userAddr)396 LITE_OS_SEC_TEXT STATIC INTPTR GetIpcKernelAddr(const LosProcessCB *pcb, INTPTR userAddr)
397 {
398 IpcPool pool = pcb->ipcInfo->pool;
399 INTPTR offset = (INTPTR)(pool.uvaddr) - (INTPTR)(pool.kvaddr);
400 return userAddr - offset;
401 }
402
CheckUsedBuffer(const VOID *node, IpcListNode **outPtr)403 LITE_OS_SEC_TEXT STATIC UINT32 CheckUsedBuffer(const VOID *node, IpcListNode **outPtr)
404 {
405 VOID *ptr = NULL;
406 LosProcessCB *pcb = OsCurrProcessGet();
407 IpcPool pool = pcb->ipcInfo->pool;
408 if ((node == NULL) || ((INTPTR)node < (INTPTR)(pool.uvaddr)) ||
409 ((INTPTR)node > (INTPTR)(pool.uvaddr) + pool.poolSize)) {
410 return -EINVAL;
411 }
412 ptr = (VOID *)GetIpcKernelAddr(pcb, (INTPTR)(node));
413 if (IsIpcNode(pcb, ptr) != TRUE) {
414 return -EFAULT;
415 }
416 *outPtr = (IpcListNode *)ptr;
417 return LOS_OK;
418 }
419
GetTid(UINT32 serviceHandle, UINT32 *taskID)420 LITE_OS_SEC_TEXT STATIC UINT32 GetTid(UINT32 serviceHandle, UINT32 *taskID)
421 {
422 if (serviceHandle >= MAX_SERVICE_NUM) {
423 return -EINVAL;
424 }
425 (VOID)LOS_MuxLock(&g_serviceHandleMapMux, LOS_WAIT_FOREVER);
426 #if (USE_TASKID_AS_HANDLE == 1)
427 *taskID = serviceHandle ? serviceHandle : g_cmsTask.taskID;
428 (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
429 return LOS_OK;
430 #else
431 if (g_serviceHandleMap[serviceHandle].status == HANDLE_REGISTED) {
432 *taskID = g_serviceHandleMap[serviceHandle].taskID;
433 (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
434 return LOS_OK;
435 }
436 (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
437 return -EINVAL;
438 #endif
439 }
440
GenerateServiceHandle(UINT32 taskID, HandleStatus status, UINT32 *serviceHandle)441 LITE_OS_SEC_TEXT STATIC UINT32 GenerateServiceHandle(UINT32 taskID, HandleStatus status, UINT32 *serviceHandle)
442 {
443 (VOID)LOS_MuxLock(&g_serviceHandleMapMux, LOS_WAIT_FOREVER);
444 #if (USE_TASKID_AS_HANDLE == 1)
445 *serviceHandle = taskID ? taskID : LOS_CurTaskIDGet(); /* if taskID is 0, return curTaskID */
446 if (*serviceHandle != g_cmsTask.taskID) {
447 (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
448 return LOS_OK;
449 }
450 #else
451 for (UINT32 i = 1; i < MAX_SERVICE_NUM; i++) {
452 if (g_serviceHandleMap[i].status == HANDLE_NOT_USED) {
453 g_serviceHandleMap[i].taskID = taskID;
454 g_serviceHandleMap[i].status = status;
455 *serviceHandle = i;
456 (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
457 return LOS_OK;
458 }
459 }
460 #endif
461 (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
462 return -EINVAL;
463 }
464
RefreshServiceHandle(UINT32 serviceHandle, UINT32 result)465 LITE_OS_SEC_TEXT STATIC VOID RefreshServiceHandle(UINT32 serviceHandle, UINT32 result)
466 {
467 #if (USE_TASKID_AS_HANDLE == 0)
468 (VOID)LOS_MuxLock(&g_serviceHandleMapMux, LOS_WAIT_FOREVER);
469 if ((result == LOS_OK) && (g_serviceHandleMap[serviceHandle].status == HANDLE_REGISTING)) {
470 g_serviceHandleMap[serviceHandle].status = HANDLE_REGISTED;
471 } else {
472 g_serviceHandleMap[serviceHandle].status = HANDLE_NOT_USED;
473 }
474 (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
475 #endif
476 }
477
AddServiceAccess(UINT32 taskID, UINT32 serviceHandle)478 LITE_OS_SEC_TEXT STATIC UINT32 AddServiceAccess(UINT32 taskID, UINT32 serviceHandle)
479 {
480 UINT32 serviceTid = 0;
481 UINT32 ret = GetTid(serviceHandle, &serviceTid);
482 if (ret != LOS_OK) {
483 PRINT_ERR("Liteipc AddServiceAccess GetTid failed\n");
484 return ret;
485 }
486
487 LosTaskCB *tcb = OS_TCB_FROM_TID(serviceTid);
488 LosProcessCB *pcb = OS_PCB_FROM_TID(taskID);
489 if ((tcb->ipcTaskInfo == NULL) || (pcb->ipcInfo == NULL)) {
490 PRINT_ERR("Liteipc AddServiceAccess ipc not create! pid %u tid %u\n", pcb->processID, tcb->taskID);
491 return -EINVAL;
492 }
493 tcb->ipcTaskInfo->accessMap[pcb->processID] = TRUE;
494 pcb->ipcInfo->access[serviceTid] = TRUE;
495 return LOS_OK;
496 }
497
HasServiceAccess(UINT32 serviceHandle)498 LITE_OS_SEC_TEXT STATIC BOOL HasServiceAccess(UINT32 serviceHandle)
499 {
500 UINT32 serviceTid = 0;
501 LosProcessCB *curr = OsCurrProcessGet();
502 UINT32 ret;
503 if (serviceHandle >= MAX_SERVICE_NUM) {
504 return FALSE;
505 }
506 if (serviceHandle == 0) {
507 return TRUE;
508 }
509 ret = GetTid(serviceHandle, &serviceTid);
510 if (ret != LOS_OK) {
511 PRINT_ERR("Liteipc HasServiceAccess GetTid failed\n");
512 return FALSE;
513 }
514 LosTaskCB *taskCB = OS_TCB_FROM_TID(serviceTid);
515 if (taskCB->processCB == (UINTPTR)curr) {
516 return TRUE;
517 }
518
519 if (taskCB->ipcTaskInfo == NULL) {
520 return FALSE;
521 }
522
523 return taskCB->ipcTaskInfo->accessMap[curr->processID];
524 }
525
SetIpcTasknull526 LITE_OS_SEC_TEXT STATIC UINT32 SetIpcTask(VOID)
527 {
528 if (OsCurrProcessGet()->ipcInfo->ipcTaskID == INVAILD_ID) {
529 OsCurrProcessGet()->ipcInfo->ipcTaskID = LOS_CurTaskIDGet();
530 return OsCurrProcessGet()->ipcInfo->ipcTaskID;
531 }
532 PRINT_ERR("Liteipc curprocess %d IpcTask already set!\n", OsCurrProcessGet()->processID);
533 return -EINVAL;
534 }
535
IsIpcTaskSetnull536 LITE_OS_SEC_TEXT BOOL IsIpcTaskSet(VOID)
537 {
538 if (OsCurrProcessGet()->ipcInfo->ipcTaskID == INVAILD_ID) {
539 return FALSE;
540 }
541 return TRUE;
542 }
543
GetIpcTaskID(LosProcessCB *pcb, UINT32 *ipcTaskID)544 LITE_OS_SEC_TEXT STATIC UINT32 GetIpcTaskID(LosProcessCB *pcb, UINT32 *ipcTaskID)
545 {
546 if (pcb->ipcInfo->ipcTaskID == INVAILD_ID) {
547 return LOS_NOK;
548 }
549 *ipcTaskID = pcb->ipcInfo->ipcTaskID;
550 return LOS_OK;
551 }
552
SendDeathMsg(UINT32 processID, UINT32 serviceHandle)553 LITE_OS_SEC_TEXT STATIC UINT32 SendDeathMsg(UINT32 processID, UINT32 serviceHandle)
554 {
555 UINT32 ipcTaskID;
556 UINT32 ret;
557 IpcContent content;
558 IpcMsg msg;
559 LosProcessCB *pcb = OS_PCB_FROM_PID(processID);
560
561 if (pcb->ipcInfo == NULL) {
562 return -EINVAL;
563 }
564
565 pcb->ipcInfo->access[serviceHandle] = FALSE;
566
567 ret = GetIpcTaskID(pcb, &ipcTaskID);
568 if (ret != LOS_OK) {
569 return -EINVAL;
570 }
571 content.flag = SEND;
572 content.outMsg = &msg;
573 (void)memset_s(content.outMsg, sizeof(IpcMsg), 0, sizeof(IpcMsg));
574 content.outMsg->type = MT_DEATH_NOTIFY;
575 content.outMsg->target.handle = ipcTaskID;
576 content.outMsg->target.token = serviceHandle;
577 content.outMsg->code = 0;
578 return LiteIpcWrite(&content);
579 }
580
LiteIpcRemoveServiceHandle(UINT32 taskID)581 LITE_OS_SEC_TEXT VOID LiteIpcRemoveServiceHandle(UINT32 taskID)
582 {
583 UINT32 j;
584 LosTaskCB *taskCB = OS_TCB_FROM_TID(taskID);
585 IpcTaskInfo *ipcTaskInfo = taskCB->ipcTaskInfo;
586 if (ipcTaskInfo == NULL) {
587 return;
588 }
589
590 #if (USE_TASKID_AS_HANDLE == 1)
591
592 UINT32 intSave;
593 LOS_DL_LIST *listHead = NULL;
594 LOS_DL_LIST *listNode = NULL;
595 IpcListNode *node = NULL;
596 LosProcessCB *pcb = OS_PCB_FROM_TCB(taskCB);
597
598 listHead = &(ipcTaskInfo->msgListHead);
599 do {
600 SCHEDULER_LOCK(intSave);
601 if (LOS_ListEmpty(listHead)) {
602 SCHEDULER_UNLOCK(intSave);
603 break;
604 } else {
605 listNode = LOS_DL_LIST_FIRST(listHead);
606 LOS_ListDelete(listNode);
607 node = LOS_DL_LIST_ENTRY(listNode, IpcListNode, listNode);
608 SCHEDULER_UNLOCK(intSave);
609 (VOID)HandleSpecialObjects(taskCB->taskID, node, TRUE);
610 (VOID)LiteIpcNodeFree(pcb, (VOID *)node);
611 }
612 } while (1);
613
614 ipcTaskInfo->accessMap[pcb->processID] = FALSE;
615 for (j = 0; j < LOSCFG_BASE_CORE_PROCESS_LIMIT; j++) {
616 if (ipcTaskInfo->accessMap[j] == TRUE) {
617 ipcTaskInfo->accessMap[j] = FALSE;
618 (VOID)SendDeathMsg(j, taskCB->taskID);
619 }
620 }
621 #else
622 (VOID)LOS_MuxLock(&g_serviceHandleMapMux, LOS_WAIT_FOREVER);
623 for (UINT32 i = 1; i < MAX_SERVICE_NUM; i++) {
624 if ((g_serviceHandleMap[i].status != HANDLE_NOT_USED) && (g_serviceHandleMap[i].taskID == taskCB->taskID)) {
625 g_serviceHandleMap[i].status = HANDLE_NOT_USED;
626 g_serviceHandleMap[i].taskID = INVAILD_ID;
627 break;
628 }
629 }
630 (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
631 /* run deathHandler */
632 if (i < MAX_SERVICE_NUM) {
633 for (j = 0; j < LOSCFG_BASE_CORE_PROCESS_LIMIT; j++) {
634 if (ipcTaskInfo->accessMap[j] == TRUE) {
635 (VOID)SendDeathMsg(j, i);
636 }
637 }
638 }
639 #endif
640
641 (VOID)LOS_MemFree(m_aucSysMem1, ipcTaskInfo);
642 taskCB->ipcTaskInfo = NULL;
643 }
644
SetCms(UINTPTR maxMsgSize)645 LITE_OS_SEC_TEXT STATIC UINT32 SetCms(UINTPTR maxMsgSize)
646 {
647 if (maxMsgSize < sizeof(IpcMsg)) {
648 return -EINVAL;
649 }
650 (VOID)LOS_MuxLock(&g_serviceHandleMapMux, LOS_WAIT_FOREVER);
651 #if (USE_TASKID_AS_HANDLE == 1)
652 if (g_cmsTask.status == HANDLE_NOT_USED) {
653 g_cmsTask.status = HANDLE_REGISTED;
654 g_cmsTask.taskID = LOS_CurTaskIDGet();
655 g_cmsTask.maxMsgSize = maxMsgSize;
656 (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
657 return LOS_OK;
658 }
659 #else
660 if (g_serviceHandleMap[0].status == HANDLE_NOT_USED) {
661 g_serviceHandleMap[0].status = HANDLE_REGISTED;
662 g_serviceHandleMap[0].taskID = LOS_CurTaskIDGet();
663 (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
664 return LOS_OK;
665 }
666 #endif
667 (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
668 return -EEXIST;
669 }
670
IsCmsSetnull671 LITE_OS_SEC_TEXT STATIC BOOL IsCmsSet(VOID)
672 {
673 BOOL ret;
674 (VOID)LOS_MuxLock(&g_serviceHandleMapMux, LOS_WAIT_FOREVER);
675 #if (USE_TASKID_AS_HANDLE == 1)
676 ret = g_cmsTask.status == HANDLE_REGISTED;
677 #else
678 ret = g_serviceHandleMap[0].status == HANDLE_REGISTED;
679 #endif
680 (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
681 return ret;
682 }
683
IsCmsTask(UINT32 taskID)684 LITE_OS_SEC_TEXT STATIC BOOL IsCmsTask(UINT32 taskID)
685 {
686 BOOL ret;
687 (VOID)LOS_MuxLock(&g_serviceHandleMapMux, LOS_WAIT_FOREVER);
688 #if (USE_TASKID_AS_HANDLE == 1)
689 ret = IsCmsSet() ? (OS_TCB_FROM_TID(taskID)->processCB == OS_TCB_FROM_TID(g_cmsTask.taskID)->processCB) : FALSE;
690 #else
691 ret = IsCmsSet() ? (OS_TCB_FROM_TID(taskID)->processCB ==
692 OS_TCB_FROM_TID(g_serviceHandleMap[0].taskID)->processCB) : FALSE;
693 #endif
694 (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
695 return ret;
696 }
697
IsTaskAlive(UINT32 taskID)698 LITE_OS_SEC_TEXT STATIC BOOL IsTaskAlive(UINT32 taskID)
699 {
700 LosTaskCB *tcb = NULL;
701 if (OS_TID_CHECK_INVALID(taskID)) {
702 return FALSE;
703 }
704 tcb = OS_TCB_FROM_TID(taskID);
705 if (OsTaskIsUnused(tcb)) {
706 return FALSE;
707 }
708 if (OsTaskIsInactive(tcb)) {
709 return FALSE;
710 }
711 if (!OsTaskIsUserMode(tcb)) {
712 return FALSE;
713 }
714 return TRUE;
715 }
716
HandleFd(const LosProcessCB *pcb, SpecialObj *obj, BOOL isRollback)717 LITE_OS_SEC_TEXT STATIC UINT32 HandleFd(const LosProcessCB *pcb, SpecialObj *obj, BOOL isRollback)
718 {
719 int ret;
720 if (isRollback == FALSE) {
721 ret = CopyFdToProc(obj->content.fd, pcb->processID);
722 if (ret < 0) {
723 return ret;
724 }
725 obj->content.fd = ret;
726 } else {
727 ret = CloseProcFd(obj->content.fd, pcb->processID);
728 if (ret < 0) {
729 return ret;
730 }
731 }
732
733 return LOS_OK;
734 }
735
HandlePtr(LosProcessCB *pcb, SpecialObj *obj, BOOL isRollback)736 LITE_OS_SEC_TEXT STATIC UINT32 HandlePtr(LosProcessCB *pcb, SpecialObj *obj, BOOL isRollback)
737 {
738 VOID *buf = NULL;
739 UINT32 ret;
740 if ((obj->content.ptr.buff == NULL) || (obj->content.ptr.buffSz == 0)) {
741 return -EINVAL;
742 }
743 if (isRollback == FALSE) {
744 if (LOS_IsUserAddress((vaddr_t)(UINTPTR)(obj->content.ptr.buff)) == FALSE) {
745 PRINT_ERR("Liteipc Bad ptr address\n");
746 return -EINVAL;
747 }
748 buf = LiteIpcNodeAlloc(pcb, obj->content.ptr.buffSz);
749 if (buf == NULL) {
750 PRINT_ERR("Liteipc DealPtr alloc mem failed\n");
751 return -EINVAL;
752 }
753 ret = copy_from_user(buf, obj->content.ptr.buff, obj->content.ptr.buffSz);
754 if (ret != LOS_OK) {
755 LiteIpcNodeFree(pcb, buf);
756 return ret;
757 }
758 obj->content.ptr.buff = (VOID *)GetIpcUserAddr(pcb, (INTPTR)buf);
759 EnableIpcNodeFreeByUser(pcb, (VOID *)buf);
760 } else {
761 buf = (VOID *)GetIpcKernelAddr(pcb, (INTPTR)obj->content.ptr.buff);
762 if (IsIpcNode(pcb, buf) == TRUE) {
763 (VOID)LiteIpcNodeFree(pcb, buf);
764 }
765 }
766 return LOS_OK;
767 }
768
HandleSvc(UINT32 dstTid, SpecialObj *obj, BOOL isRollback)769 LITE_OS_SEC_TEXT STATIC UINT32 HandleSvc(UINT32 dstTid, SpecialObj *obj, BOOL isRollback)
770 {
771 UINT32 taskID = 0;
772 if (isRollback == FALSE) {
773 if (obj->content.svc.handle == -1) {
774 if (obj->content.svc.token != 1) {
775 PRINT_ERR("Liteipc HandleSvc wrong svc token\n");
776 return -EINVAL;
777 }
778 UINT32 selfTid = LOS_CurTaskIDGet();
779 LosTaskCB *tcb = OS_TCB_FROM_TID(selfTid);
780 if (tcb->ipcTaskInfo == NULL) {
781 tcb->ipcTaskInfo = LiteIpcTaskInit();
782 }
783 uint32_t serviceHandle = 0;
784 UINT32 ret = GenerateServiceHandle(selfTid, HANDLE_REGISTED, &serviceHandle);
785 if (ret != LOS_OK) {
786 PRINT_ERR("Liteipc GenerateServiceHandle failed.\n");
787 return ret;
788 }
789 obj->content.svc.handle = serviceHandle;
790 (VOID)LOS_MuxLock(&g_serviceHandleMapMux, LOS_WAIT_FOREVER);
791 AddServiceAccess(dstTid, serviceHandle);
792 (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
793 }
794 if (IsTaskAlive(obj->content.svc.handle) == FALSE) {
795 PRINT_ERR("Liteipc HandleSvc wrong svctid\n");
796 return -EINVAL;
797 }
798 if (HasServiceAccess(obj->content.svc.handle) == FALSE) {
799 PRINT_ERR("Liteipc %s, %d, svchandle:%d, tid:%d\n", __FUNCTION__, __LINE__, obj->content.svc.handle, LOS_CurTaskIDGet());
800 return -EACCES;
801 }
802 LosTaskCB *taskCb = OS_TCB_FROM_TID(obj->content.svc.handle);
803 if (taskCb->ipcTaskInfo == NULL) {
804 taskCb->ipcTaskInfo = LiteIpcTaskInit();
805 }
806 if (GetTid(obj->content.svc.handle, &taskID) == 0) {
807 AddServiceAccess(dstTid, obj->content.svc.handle);
808 }
809 }
810 return LOS_OK;
811 }
812
HandleObj(UINT32 dstTid, SpecialObj *obj, BOOL isRollback)813 LITE_OS_SEC_TEXT STATIC UINT32 HandleObj(UINT32 dstTid, SpecialObj *obj, BOOL isRollback)
814 {
815 UINT32 ret;
816 LosProcessCB *pcb = OS_PCB_FROM_TID(dstTid);
817 switch (obj->type) {
818 case OBJ_FD:
819 ret = HandleFd(pcb, obj, isRollback);
820 break;
821 case OBJ_PTR:
822 ret = HandlePtr(pcb, obj, isRollback);
823 break;
824 case OBJ_SVC:
825 ret = HandleSvc(dstTid, (SpecialObj *)obj, isRollback);
826 break;
827 default:
828 ret = -EINVAL;
829 break;
830 }
831 return ret;
832 }
833
HandleSpecialObjects(UINT32 dstTid, IpcListNode *node, BOOL isRollback)834 LITE_OS_SEC_TEXT STATIC UINT32 HandleSpecialObjects(UINT32 dstTid, IpcListNode *node, BOOL isRollback)
835 {
836 UINT32 ret = LOS_OK;
837 IpcMsg *msg = &(node->msg);
838 INT32 i;
839 SpecialObj *obj = NULL;
840 UINT32 *offset = (UINT32 *)(UINTPTR)(msg->offsets);
841 if (isRollback) {
842 i = msg->spObjNum;
843 goto EXIT;
844 }
845 for (i = 0; i < msg->spObjNum; i++) {
846 if (offset[i] > msg->dataSz - sizeof(SpecialObj)) {
847 ret = -EINVAL;
848 goto EXIT;
849 }
850 if ((i > 0) && (offset[i] < offset[i - 1] + sizeof(SpecialObj))) {
851 ret = -EINVAL;
852 goto EXIT;
853 }
854 obj = (SpecialObj *)((UINTPTR)msg->data + offset[i]);
855 if (obj == NULL) {
856 ret = -EINVAL;
857 goto EXIT;
858 }
859 ret = HandleObj(dstTid, obj, FALSE);
860 if (ret != LOS_OK) {
861 goto EXIT;
862 }
863 }
864 return LOS_OK;
865 EXIT:
866 for (i--; i >= 0; i--) {
867 obj = (SpecialObj *)((UINTPTR)msg->data + offset[i]);
868 (VOID)HandleObj(dstTid, obj, TRUE);
869 }
870 return ret;
871 }
872
CheckMsgSize(IpcMsg *msg)873 LITE_OS_SEC_TEXT STATIC UINT32 CheckMsgSize(IpcMsg *msg)
874 {
875 UINT64 totalSize;
876 UINT32 i;
877 UINT32 *offset = (UINT32 *)(UINTPTR)(msg->offsets);
878 SpecialObj *obj = NULL;
879 if (msg->target.handle != 0) {
880 return LOS_OK;
881 }
882 /* msg send to cms, check the msg size */
883 totalSize = (UINT64)sizeof(IpcMsg) + msg->dataSz + msg->spObjNum * sizeof(UINT32);
884 for (i = 0; i < msg->spObjNum; i++) {
885 if (offset[i] > msg->dataSz - sizeof(SpecialObj)) {
886 return -EINVAL;
887 }
888 if ((i > 0) && (offset[i] < offset[i - 1] + sizeof(SpecialObj))) {
889 return -EINVAL;
890 }
891 obj = (SpecialObj *)((UINTPTR)msg->data + offset[i]);
892 if (obj == NULL) {
893 return -EINVAL;
894 }
895 if (obj->type == OBJ_PTR) {
896 totalSize += obj->content.ptr.buffSz;
897 }
898 }
899 (VOID)LOS_MuxLock(&g_serviceHandleMapMux, LOS_WAIT_FOREVER);
900 if (totalSize > g_cmsTask.maxMsgSize) {
901 (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
902 return -EINVAL;
903 }
904 (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
905 return LOS_OK;
906 }
907
CopyDataFromUser(IpcListNode *node, UINT32 bufSz, const IpcMsg *msg)908 LITE_OS_SEC_TEXT STATIC UINT32 CopyDataFromUser(IpcListNode *node, UINT32 bufSz, const IpcMsg *msg)
909 {
910 UINT32 ret;
911 ret = (UINT32)memcpy_s((VOID *)(&node->msg), bufSz - sizeof(LOS_DL_LIST), (const VOID *)msg, sizeof(IpcMsg));
912 if (ret != LOS_OK) {
913 PRINT_DEBUG("%s, %d, %u\n", __FUNCTION__, __LINE__, ret);
914 return ret;
915 }
916
917 if (msg->dataSz) {
918 node->msg.data = (VOID *)((UINTPTR)node + sizeof(IpcListNode));
919 ret = copy_from_user((VOID *)(node->msg.data), msg->data, msg->dataSz);
920 if (ret != LOS_OK) {
921 PRINT_DEBUG("%s, %d\n", __FUNCTION__, __LINE__);
922 return ret;
923 }
924 } else {
925 node->msg.data = NULL;
926 }
927
928 if (msg->spObjNum) {
929 node->msg.offsets = (VOID *)((UINTPTR)node + sizeof(IpcListNode) + msg->dataSz);
930 ret = copy_from_user((VOID *)(node->msg.offsets), msg->offsets, msg->spObjNum * sizeof(UINT32));
931 if (ret != LOS_OK) {
932 PRINT_DEBUG("%s, %d, %x, %x, %d\n", __FUNCTION__, __LINE__, node->msg.offsets, msg->offsets, msg->spObjNum);
933 return ret;
934 }
935 } else {
936 node->msg.offsets = NULL;
937 }
938 ret = CheckMsgSize(&node->msg);
939 if (ret != LOS_OK) {
940 PRINT_DEBUG("%s, %d\n", __FUNCTION__, __LINE__);
941 return ret;
942 }
943 node->msg.taskID = LOS_CurTaskIDGet();
944 node->msg.processID = OsCurrProcessGet()->processID;
945 #ifdef LOSCFG_SECURITY_CAPABILITY
946 node->msg.userID = OsCurrProcessGet()->user->userID;
947 node->msg.gid = OsCurrProcessGet()->user->gid;
948 #endif
949 return LOS_OK;
950 }
951
IsValidReply(const IpcContent *content)952 LITE_OS_SEC_TEXT STATIC BOOL IsValidReply(const IpcContent *content)
953 {
954 LosProcessCB *curr = OsCurrProcessGet();
955 IpcListNode *node = (IpcListNode *)GetIpcKernelAddr(curr, (INTPTR)(content->buffToFree));
956 IpcMsg *requestMsg = &node->msg;
957 IpcMsg *replyMsg = content->outMsg;
958 UINT32 reqDstTid = 0;
959 /* Check whether the reply matches the request */
960 if ((requestMsg->type != MT_REQUEST) ||
961 (requestMsg->flag == LITEIPC_FLAG_ONEWAY) ||
962 (replyMsg->timestamp != requestMsg->timestamp) ||
963 (replyMsg->target.handle != requestMsg->taskID) ||
964 (GetTid(requestMsg->target.handle, &reqDstTid) != 0) ||
965 (OS_TCB_FROM_TID(reqDstTid)->processCB != (UINTPTR)curr)) {
966 return FALSE;
967 }
968 return TRUE;
969 }
970
CheckPara(IpcContent *content, UINT32 *dstTid)971 LITE_OS_SEC_TEXT STATIC UINT32 CheckPara(IpcContent *content, UINT32 *dstTid)
972 {
973 UINT32 ret;
974 IpcMsg *msg = content->outMsg;
975 UINT32 flag = content->flag;
976 #if (USE_TIMESTAMP == 1)
977 UINT64 now = LOS_CurrNanosec();
978 #endif
979 if (((msg->dataSz > 0) && (msg->data == NULL)) ||
980 ((msg->spObjNum > 0) && (msg->offsets == NULL)) ||
981 (msg->dataSz > IPC_MSG_DATA_SZ_MAX) ||
982 (msg->spObjNum > IPC_MSG_OBJECT_NUM_MAX) ||
983 (msg->dataSz < msg->spObjNum * sizeof(SpecialObj))) {
984 return -EINVAL;
985 }
986 switch (msg->type) {
987 case MT_REQUEST:
988 if (HasServiceAccess(msg->target.handle)) {
989 ret = GetTid(msg->target.handle, dstTid);
990 if (ret != LOS_OK) {
991 return -EINVAL;
992 }
993 } else {
994 PRINT_ERR("Liteipc %s, %d\n", __FUNCTION__, __LINE__);
995 return -EACCES;
996 }
997 #if (USE_TIMESTAMP == 1)
998 msg->timestamp = now;
999 #endif
1000 break;
1001 case MT_REPLY:
1002 case MT_FAILED_REPLY:
1003 if ((flag & BUFF_FREE) != BUFF_FREE) {
1004 return -EINVAL;
1005 }
1006 if (!IsValidReply(content)) {
1007 return -EINVAL;
1008 }
1009 #if (USE_TIMESTAMP == 1)
1010 if (now > msg->timestamp + LITEIPC_TIMEOUT_NS) {
1011 #ifdef LOSCFG_KERNEL_HOOK
1012 ret = GetTid(msg->target.handle, dstTid);
1013 if (ret != LOS_OK) {
1014 *dstTid = INVAILD_ID;
1015 }
1016 #endif
1017 OsHookCall(LOS_HOOK_TYPE_IPC_WRITE_DROP, msg, *dstTid,
1018 (*dstTid == INVAILD_ID) ? INVAILD_ID : OS_PCB_FROM_TID(*dstTid)->processID, 0);
1019 PRINT_ERR("Liteipc A timeout reply, request timestamp:%lld, now:%lld\n", msg->timestamp, now);
1020 return -ETIME;
1021 }
1022 #endif
1023 *dstTid = msg->target.handle;
1024 break;
1025 case MT_DEATH_NOTIFY:
1026 *dstTid = msg->target.handle;
1027 #if (USE_TIMESTAMP == 1)
1028 msg->timestamp = now;
1029 #endif
1030 break;
1031 default:
1032 PRINT_DEBUG("Unknown msg type:%d\n", msg->type);
1033 return -EINVAL;
1034 }
1035
1036 if (IsTaskAlive(*dstTid) == FALSE) {
1037 return -EINVAL;
1038 }
1039 return LOS_OK;
1040 }
1041
LiteIpcWrite(IpcContent *content)1042 LITE_OS_SEC_TEXT STATIC UINT32 LiteIpcWrite(IpcContent *content)
1043 {
1044 UINT32 ret, intSave;
1045 UINT32 dstTid;
1046
1047 IpcMsg *msg = content->outMsg;
1048
1049 ret = CheckPara(content, &dstTid);
1050 if (ret != LOS_OK) {
1051 return ret;
1052 }
1053
1054 LosTaskCB *tcb = OS_TCB_FROM_TID(dstTid);
1055 LosProcessCB *pcb = OS_PCB_FROM_TCB(tcb);
1056 if (pcb->ipcInfo == NULL) {
1057 PRINT_ERR("pid %u Liteipc not create\n", pcb->processID);
1058 return -EINVAL;
1059 }
1060
1061 UINT32 bufSz = sizeof(IpcListNode) + msg->dataSz + msg->spObjNum * sizeof(UINT32);
1062 IpcListNode *buf = (IpcListNode *)LiteIpcNodeAlloc(pcb, bufSz);
1063 if (buf == NULL) {
1064 PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
1065 return -ENOMEM;
1066 }
1067 ret = CopyDataFromUser(buf, bufSz, (const IpcMsg *)msg);
1068 if (ret != LOS_OK) {
1069 PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
1070 goto ERROR_COPY;
1071 }
1072
1073 if (tcb->ipcTaskInfo == NULL) {
1074 tcb->ipcTaskInfo = LiteIpcTaskInit();
1075 }
1076
1077 ret = HandleSpecialObjects(dstTid, buf, FALSE);
1078 if (ret != LOS_OK) {
1079 PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
1080 goto ERROR_COPY;
1081 }
1082 /* add data to list and wake up dest task */
1083 SCHEDULER_LOCK(intSave);
1084 LOS_ListTailInsert(&(tcb->ipcTaskInfo->msgListHead), &(buf->listNode));
1085 OsHookCall(LOS_HOOK_TYPE_IPC_WRITE, &buf->msg, dstTid, pcb->processID, tcb->waitFlag);
1086 if (tcb->waitFlag == OS_TASK_WAIT_LITEIPC) {
1087 OsTaskWakeClearPendMask(tcb);
1088 tcb->ops->wake(tcb);
1089 SCHEDULER_UNLOCK(intSave);
1090 LOS_MpSchedule(OS_MP_CPU_ALL);
1091 LOS_Schedule();
1092 } else {
1093 SCHEDULER_UNLOCK(intSave);
1094 }
1095 return LOS_OK;
1096 ERROR_COPY:
1097 LiteIpcNodeFree(pcb, buf);
1098 return ret;
1099 }
1100
CheckReceivedMsg(IpcListNode *node, IpcContent *content, LosTaskCB *tcb)1101 LITE_OS_SEC_TEXT STATIC UINT32 CheckReceivedMsg(IpcListNode *node, IpcContent *content, LosTaskCB *tcb)
1102 {
1103 UINT32 ret = LOS_OK;
1104 if (node == NULL) {
1105 return -EINVAL;
1106 }
1107 switch (node->msg.type) {
1108 case MT_REQUEST:
1109 if ((content->flag & SEND) == SEND) {
1110 PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
1111 ret = -EINVAL;
1112 }
1113 break;
1114 case MT_FAILED_REPLY:
1115 ret = -ENOENT;
1116 /* fall-through */
1117 case MT_REPLY:
1118 if ((content->flag & SEND) != SEND) {
1119 PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
1120 ret = -EINVAL;
1121 }
1122 #if (USE_TIMESTAMP == 1)
1123 if (node->msg.timestamp != content->outMsg->timestamp) {
1124 PRINT_ERR("Receive a unmatch reply, drop it\n");
1125 ret = -EINVAL;
1126 }
1127 #else
1128 if ((node->msg.code != content->outMsg->code) ||
1129 (node->msg.target.token != content->outMsg->target.token)) {
1130 PRINT_ERR("Receive a unmatch reply, drop it\n");
1131 ret = -EINVAL;
1132 }
1133 #endif
1134 break;
1135 case MT_DEATH_NOTIFY:
1136 break;
1137 default:
1138 PRINT_ERR("Unknown msg type:%d\n", node->msg.type);
1139 ret = -EINVAL;
1140 }
1141 if (ret != LOS_OK) {
1142 OsHookCall(LOS_HOOK_TYPE_IPC_READ_DROP, &node->msg, tcb->waitFlag);
1143 (VOID)HandleSpecialObjects(LOS_CurTaskIDGet(), node, TRUE);
1144 (VOID)LiteIpcNodeFree(OsCurrProcessGet(), (VOID *)node);
1145 } else {
1146 OsHookCall(LOS_HOOK_TYPE_IPC_READ, &node->msg, tcb->waitFlag);
1147 }
1148 return ret;
1149 }
1150
LiteIpcRead(IpcContent *content)1151 LITE_OS_SEC_TEXT STATIC UINT32 LiteIpcRead(IpcContent *content)
1152 {
1153 UINT32 intSave, ret;
1154 UINT32 selfTid = LOS_CurTaskIDGet();
1155 LOS_DL_LIST *listHead = NULL;
1156 LOS_DL_LIST *listNode = NULL;
1157 IpcListNode *node = NULL;
1158 UINT32 syncFlag = (content->flag & SEND) && (content->flag & RECV);
1159 UINT32 timeout = syncFlag ? LOS_MS2Tick(LITEIPC_TIMEOUT_MS) : LOS_WAIT_FOREVER;
1160
1161 LosTaskCB *tcb = OS_TCB_FROM_TID(selfTid);
1162 if (tcb->ipcTaskInfo == NULL) {
1163 tcb->ipcTaskInfo = LiteIpcTaskInit();
1164 }
1165
1166 listHead = &(tcb->ipcTaskInfo->msgListHead);
1167 do {
1168 SCHEDULER_LOCK(intSave);
1169 if (LOS_ListEmpty(listHead)) {
1170 OsTaskWaitSetPendMask(OS_TASK_WAIT_LITEIPC, OS_INVALID_VALUE, timeout);
1171 OsHookCall(LOS_HOOK_TYPE_IPC_TRY_READ, syncFlag ? MT_REPLY : MT_REQUEST, tcb->waitFlag);
1172 ret = tcb->ops->wait(tcb, &g_ipcPendlist, timeout);
1173 if (ret == LOS_ERRNO_TSK_TIMEOUT) {
1174 OsHookCall(LOS_HOOK_TYPE_IPC_READ_TIMEOUT, syncFlag ? MT_REPLY : MT_REQUEST, tcb->waitFlag);
1175 SCHEDULER_UNLOCK(intSave);
1176 return -ETIME;
1177 }
1178
1179 if (OsTaskIsKilled(tcb)) {
1180 OsHookCall(LOS_HOOK_TYPE_IPC_KILL, syncFlag ? MT_REPLY : MT_REQUEST, tcb->waitFlag);
1181 SCHEDULER_UNLOCK(intSave);
1182 return -ERFKILL;
1183 }
1184
1185 SCHEDULER_UNLOCK(intSave);
1186 } else {
1187 listNode = LOS_DL_LIST_FIRST(listHead);
1188 LOS_ListDelete(listNode);
1189 node = LOS_DL_LIST_ENTRY(listNode, IpcListNode, listNode);
1190 SCHEDULER_UNLOCK(intSave);
1191 ret = CheckReceivedMsg(node, content, tcb);
1192 if (ret == LOS_OK) {
1193 break;
1194 }
1195 if (ret == -ENOENT) { /* It means that we've received a failed reply */
1196 return ret;
1197 }
1198 }
1199 } while (1);
1200 node->msg.data = (VOID *)GetIpcUserAddr(OsCurrProcessGet(), (INTPTR)(node->msg.data));
1201 node->msg.offsets = (VOID *)GetIpcUserAddr(OsCurrProcessGet(), (INTPTR)(node->msg.offsets));
1202 content->inMsg = (VOID *)GetIpcUserAddr(OsCurrProcessGet(), (INTPTR)(&(node->msg)));
1203 EnableIpcNodeFreeByUser(OsCurrProcessGet(), (VOID *)node);
1204 return LOS_OK;
1205 }
1206
LiteIpcMsgHandle(IpcContent *con)1207 LITE_OS_SEC_TEXT STATIC UINT32 LiteIpcMsgHandle(IpcContent *con)
1208 {
1209 UINT32 ret = LOS_OK;
1210 IpcContent localContent;
1211 IpcContent *content = &localContent;
1212 IpcMsg localMsg;
1213 IpcMsg *msg = &localMsg;
1214 IpcListNode *nodeNeedFree = NULL;
1215
1216 if (copy_from_user((void *)content, (const void *)con, sizeof(IpcContent)) != LOS_OK) {
1217 PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
1218 return -EINVAL;
1219 }
1220
1221 if ((content->flag & BUFF_FREE) == BUFF_FREE) {
1222 ret = CheckUsedBuffer(content->buffToFree, &nodeNeedFree);
1223 if (ret != LOS_OK) {
1224 PRINT_ERR("CheckUsedBuffer failed:%d\n", ret);
1225 return ret;
1226 }
1227 }
1228
1229 if ((content->flag & SEND) == SEND) {
1230 if (content->outMsg == NULL) {
1231 PRINT_ERR("content->outmsg is null\n");
1232 ret = -EINVAL;
1233 goto BUFFER_FREE;
1234 }
1235 if (copy_from_user((void *)msg, (const void *)content->outMsg, sizeof(IpcMsg)) != LOS_OK) {
1236 PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
1237 ret = -EINVAL;
1238 goto BUFFER_FREE;
1239 }
1240 content->outMsg = msg;
1241 if ((content->outMsg->type < 0) || (content->outMsg->type >= MT_DEATH_NOTIFY)) {
1242 PRINT_ERR("LiteIpc unknown msg type:%d\n", content->outMsg->type);
1243 ret = -EINVAL;
1244 goto BUFFER_FREE;
1245 }
1246 ret = LiteIpcWrite(content);
1247 if (ret != LOS_OK) {
1248 PRINT_ERR("LiteIpcWrite failed\n");
1249 goto BUFFER_FREE;
1250 }
1251 }
1252 BUFFER_FREE:
1253 if (nodeNeedFree != NULL) {
1254 UINT32 freeRet = LiteIpcNodeFree(OsCurrProcessGet(), nodeNeedFree);
1255 ret = (freeRet == LOS_OK) ? ret : freeRet;
1256 }
1257 if (ret != LOS_OK) {
1258 return ret;
1259 }
1260
1261 if ((content->flag & RECV) == RECV) {
1262 ret = LiteIpcRead(content);
1263 if (ret != LOS_OK) {
1264 PRINT_ERR("LiteIpcRead failed ERROR: %d\n", (INT32)ret);
1265 return ret;
1266 }
1267 UINT32 offset = LOS_OFF_SET_OF(IpcContent, inMsg);
1268 ret = copy_to_user((char*)con + offset, (char*)content + offset, sizeof(IpcMsg *));
1269 if (ret != LOS_OK) {
1270 PRINT_ERR("%s, %d, %d\n", __FUNCTION__, __LINE__, ret);
1271 return -EINVAL;
1272 }
1273 }
1274 return ret;
1275 }
1276
HandleCmsCmd(CmsCmdContent *content)1277 LITE_OS_SEC_TEXT STATIC UINT32 HandleCmsCmd(CmsCmdContent *content)
1278 {
1279 UINT32 ret = LOS_OK;
1280 CmsCmdContent localContent;
1281 if (content == NULL) {
1282 return -EINVAL;
1283 }
1284 if (IsCmsTask(LOS_CurTaskIDGet()) == FALSE) {
1285 return -EACCES;
1286 }
1287 if (copy_from_user((void *)(&localContent), (const void *)content, sizeof(CmsCmdContent)) != LOS_OK) {
1288 PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
1289 return -EINVAL;
1290 }
1291 switch (localContent.cmd) {
1292 case CMS_GEN_HANDLE:
1293 if ((localContent.taskID != 0) && (IsTaskAlive(localContent.taskID) == FALSE)) {
1294 return -EINVAL;
1295 }
1296 ret = GenerateServiceHandle(localContent.taskID, HANDLE_REGISTED, &(localContent.serviceHandle));
1297 if (ret == LOS_OK) {
1298 ret = copy_to_user((void *)content, (const void *)(&localContent), sizeof(CmsCmdContent));
1299 }
1300 (VOID)LOS_MuxLock(&g_serviceHandleMapMux, LOS_WAIT_FOREVER);
1301 AddServiceAccess(g_cmsTask.taskID, localContent.serviceHandle);
1302 (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
1303 break;
1304 case CMS_REMOVE_HANDLE:
1305 if (localContent.serviceHandle >= MAX_SERVICE_NUM) {
1306 return -EINVAL;
1307 }
1308 RefreshServiceHandle(localContent.serviceHandle, -1);
1309 break;
1310 case CMS_ADD_ACCESS:
1311 if (IsTaskAlive(localContent.taskID) == FALSE) {
1312 return -EINVAL;
1313 }
1314 return AddServiceAccess(localContent.taskID, localContent.serviceHandle);
1315 default:
1316 PRINT_DEBUG("Unknown cmd cmd:%d\n", localContent.cmd);
1317 return -EINVAL;
1318 }
1319 return ret;
1320 }
1321
HandleGetVersion(IpcVersion *version)1322 LITE_OS_SEC_TEXT STATIC UINT32 HandleGetVersion(IpcVersion *version)
1323 {
1324 UINT32 ret = LOS_OK;
1325 IpcVersion localIpcVersion;
1326 if (version == NULL) {
1327 return -EINVAL;
1328 }
1329
1330 localIpcVersion.driverVersion = DRIVER_VERSION;
1331 ret = copy_to_user((void *)version, (const void *)(&localIpcVersion), sizeof(IpcVersion));
1332 if (ret != LOS_OK) {
1333 PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
1334 }
1335 return ret;
1336 }
1337
LiteIpcIoctl(struct file *filep, int cmd, unsigned long arg)1338 LITE_OS_SEC_TEXT int LiteIpcIoctl(struct file *filep, int cmd, unsigned long arg)
1339 {
1340 UINT32 ret = LOS_OK;
1341 LosProcessCB *pcb = OsCurrProcessGet();
1342 ProcIpcInfo *ipcInfo = pcb->ipcInfo;
1343
1344 if (ipcInfo == NULL) {
1345 PRINT_ERR("Liteipc pool not create!\n");
1346 return -EINVAL;
1347 }
1348
1349 if (IsPoolMapped(ipcInfo) == FALSE) {
1350 PRINT_ERR("Liteipc Ipc pool not init, need to mmap first!\n");
1351 return -ENOMEM;
1352 }
1353
1354 switch (cmd) {
1355 case IPC_SET_CMS:
1356 return (INT32)SetCms(arg);
1357 case IPC_CMS_CMD:
1358 return (INT32)HandleCmsCmd((CmsCmdContent *)(UINTPTR)arg);
1359 case IPC_GET_VERSION:
1360 return (INT32)HandleGetVersion((IpcVersion *)(UINTPTR)arg);
1361 case IPC_SET_IPC_THREAD:
1362 if (IsCmsSet() == FALSE) {
1363 PRINT_ERR("Liteipc ServiceManager not set!\n");
1364 return -EINVAL;
1365 }
1366 return (INT32)SetIpcTask();
1367 case IPC_SEND_RECV_MSG:
1368 if (arg == 0) {
1369 return -EINVAL;
1370 }
1371 if (IsCmsSet() == FALSE) {
1372 PRINT_ERR("Liteipc ServiceManager not set!\n");
1373 return -EINVAL;
1374 }
1375 ret = LiteIpcMsgHandle((IpcContent *)(UINTPTR)arg);
1376 if (ret != LOS_OK) {
1377 return (INT32)ret;
1378 }
1379 break;
1380 default:
1381 PRINT_ERR("Unknown liteipc ioctl cmd:%d\n", cmd);
1382 return -EINVAL;
1383 }
1384 return (INT32)ret;
1385 }
1386