hm_liteipc.c 43.9 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
Z
zhushengle 已提交
3
 * Copyright (c) 2020-2023 Huawei Device Co., Ltd. All rights reserved.
W
wenjun 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "hm_liteipc.h"
#include "linux/kernel.h"
M
mucor 已提交
34 35
#include "fs/file.h"
#include "fs/driver.h"
36
#include "los_init.h"
W
wenjun 已提交
37 38 39
#include "los_mp.h"
#include "los_mux.h"
#include "los_process_pri.h"
M
mamingshuai 已提交
40
#include "los_sched_pri.h"
W
wenjun 已提交
41 42
#include "los_spinlock.h"
#include "los_task_pri.h"
43
#include "los_vm_lock.h"
W
wenjun 已提交
44 45
#include "los_vm_map.h"
#include "los_vm_page.h"
Z
zhOu 已提交
46
#include "los_vm_phys.h"
L
LiteOS2021 已提交
47
#include "los_hook.h"
W
wenjun 已提交
48

C
Caoruihong 已提交
49 50
#define USE_TASKID_AS_HANDLE 1
#define USE_MMAP 1
51 52
#define IPC_IO_DATA_MAX 8192UL
#define IPC_MSG_DATA_SZ_MAX (IPC_IO_DATA_MAX * sizeof(SpecialObj) / (sizeof(SpecialObj) + sizeof(size_t)))
L
laokz 已提交
53
#define IPC_MSG_OBJECT_NUM_MAX (IPC_MSG_DATA_SZ_MAX / sizeof(SpecialObj))
W
wenjun 已提交
54 55 56 57 58 59 60 61 62 63 64 65

#define LITE_IPC_POOL_NAME "liteipc"
#define LITE_IPC_POOL_PAGE_MAX_NUM 64 /* 256KB */
#define LITE_IPC_POOL_PAGE_DEFAULT_NUM 16 /* 64KB */
#define LITE_IPC_POOL_MAX_SIZE (LITE_IPC_POOL_PAGE_MAX_NUM << PAGE_SHIFT)
#define LITE_IPC_POOL_DEFAULT_SIZE (LITE_IPC_POOL_PAGE_DEFAULT_NUM << PAGE_SHIFT)
#define LITE_IPC_POOL_UVADDR 0x10000000
#define INVAILD_ID (-1)

#define LITEIPC_TIMEOUT_MS 5000UL
#define LITEIPC_TIMEOUT_NS 5000000000ULL

66 67 68 69
#define MAJOR_VERSION (2)
#define MINOR_VERSION (0)
#define DRIVER_VERSION (MAJOR_VERSION | MINOR_VERSION << 16)

W
wenjun 已提交
70 71 72 73 74
typedef struct {
    LOS_DL_LIST list;
    VOID *ptr;
} IpcUsedNode;

C
Caoruihong 已提交
75
STATIC LosMux g_serviceHandleMapMux;
C
Caoruihong 已提交
76
#if (USE_TASKID_AS_HANDLE == 1)
C
Caoruihong 已提交
77
STATIC HandleInfo g_cmsTask;
W
wenjun 已提交
78
#else
C
Caoruihong 已提交
79
STATIC HandleInfo g_serviceHandleMap[MAX_SERVICE_NUM];
W
wenjun 已提交
80 81 82 83 84 85 86 87
#endif
STATIC LOS_DL_LIST g_ipcPendlist;

/* ipc lock */
SPIN_LOCK_INIT(g_ipcSpin);
#define IPC_LOCK(state)       LOS_SpinLockSave(&g_ipcSpin, &(state))
#define IPC_UNLOCK(state)     LOS_SpinUnlockRestore(&g_ipcSpin, state)

W
wangchenyang 已提交
88 89 90 91
STATIC int LiteIpcOpen(struct file *filep);
STATIC int LiteIpcClose(struct file *filep);
STATIC int LiteIpcIoctl(struct file *filep, int cmd, unsigned long arg);
STATIC int LiteIpcMmap(struct file* filep, LosVmMapRegion *region);
W
wenjun 已提交
92 93 94
STATIC UINT32 LiteIpcWrite(IpcContent *content);
STATIC UINT32 GetTid(UINT32 serviceHandle, UINT32 *taskID);
STATIC UINT32 HandleSpecialObjects(UINT32 dstTid, IpcListNode *node, BOOL isRollback);
Z
zhushengle 已提交
95
STATIC ProcIpcInfo *LiteIpcPoolCreate(VOID);
W
wenjun 已提交
96 97

STATIC const struct file_operations_vfs g_liteIpcFops = {
W
wangchenyang 已提交
98 99 100 101
    .open = LiteIpcOpen,   /* open */
    .close = LiteIpcClose,  /* close */
    .ioctl = LiteIpcIoctl,  /* ioctl */
    .mmap = LiteIpcMmap,   /* mmap */
W
wenjun 已提交
102 103
};

104
LITE_OS_SEC_TEXT_INIT UINT32 OsLiteIpcInit(VOID)
W
wenjun 已提交
105
{
Z
zhushengle 已提交
106
    UINT32 ret;
C
Caoruihong 已提交
107
#if (USE_TASKID_AS_HANDLE == 1)
W
wenjun 已提交
108 109
    g_cmsTask.status = HANDLE_NOT_USED;
#else
A
arvinzzz 已提交
110
    (void)memset_s(g_serviceHandleMap, sizeof(g_serviceHandleMap), 0, sizeof(g_serviceHandleMap));
W
wenjun 已提交
111 112 113 114 115
#endif
    ret = LOS_MuxInit(&g_serviceHandleMapMux, NULL);
    if (ret != LOS_OK) {
        return ret;
    }
M
mamingshuai 已提交
116
    ret = (UINT32)register_driver(LITEIPC_DRIVER, &g_liteIpcFops, LITEIPC_DRIVER_MODE, NULL);
W
wenjun 已提交
117 118 119 120
    if (ret != LOS_OK) {
        PRINT_ERR("register lite_ipc driver failed:%d\n", ret);
    }
    LOS_ListInit(&(g_ipcPendlist));
L
LiteOS2021 已提交
121

W
wenjun 已提交
122 123 124
    return ret;
}

125 126
LOS_MODULE_INIT(OsLiteIpcInit, LOS_INIT_LEVEL_KMOD_EXTENDED);

W
wangchenyang 已提交
127
LITE_OS_SEC_TEXT STATIC int LiteIpcOpen(struct file *filep)
W
wenjun 已提交
128
{
Z
zhushengle 已提交
129 130 131 132 133 134 135 136 137 138
    LosProcessCB *pcb = OsCurrProcessGet();
    if (pcb->ipcInfo != NULL) {
        return 0;
    }

    pcb->ipcInfo = LiteIpcPoolCreate();
    if (pcb->ipcInfo == NULL) {
        return -ENOMEM;
    }

W
wenjun 已提交
139 140 141
    return 0;
}

W
wangchenyang 已提交
142
LITE_OS_SEC_TEXT STATIC int LiteIpcClose(struct file *filep)
W
wenjun 已提交
143 144 145 146
{
    return 0;
}

Z
zhushengle 已提交
147
LITE_OS_SEC_TEXT STATIC BOOL IsPoolMapped(ProcIpcInfo *ipcInfo)
W
wenjun 已提交
148
{
Z
zhushengle 已提交
149 150
    return (ipcInfo->pool.uvaddr != NULL) && (ipcInfo->pool.kvaddr != NULL) &&
        (ipcInfo->pool.poolSize != 0);
W
wenjun 已提交
151 152 153 154 155 156 157 158 159
}

LITE_OS_SEC_TEXT STATIC INT32 DoIpcMmap(LosProcessCB *pcb, LosVmMapRegion *region)
{
    UINT32 i;
    INT32 ret = 0;
    PADDR_T pa;
    UINT32 uflags = VM_MAP_REGION_FLAG_PERM_READ | VM_MAP_REGION_FLAG_PERM_USER;
    LosVmPage *vmPage = NULL;
Z
zhushengle 已提交
160 161
    VADDR_T uva = (VADDR_T)(UINTPTR)pcb->ipcInfo->pool.uvaddr;
    VADDR_T kva = (VADDR_T)(UINTPTR)pcb->ipcInfo->pool.kvaddr;
W
wenjun 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

    (VOID)LOS_MuxAcquire(&pcb->vmSpace->regionMux);

    for (i = 0; i < (region->range.size >> PAGE_SHIFT); i++) {
        pa = LOS_PaddrQuery((VOID *)(UINTPTR)(kva + (i << PAGE_SHIFT)));
        if (pa == 0) {
            PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
            ret = -EINVAL;
            break;
        }
        vmPage = LOS_VmPageGet(pa);
        if (vmPage == NULL) {
            PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
            ret = -EINVAL;
            break;
        }
        STATUS_T err = LOS_ArchMmuMap(&pcb->vmSpace->archMmu, uva + (i << PAGE_SHIFT), pa, 1, uflags);
        if (err < 0) {
            ret = err;
            PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
            break;
        }
    }
    /* if any failure happened, rollback */
    if (i != (region->range.size >> PAGE_SHIFT)) {
        while (i--) {
            pa = LOS_PaddrQuery((VOID *)(UINTPTR)(kva + (i << PAGE_SHIFT)));
            vmPage = LOS_VmPageGet(pa);
            (VOID)LOS_ArchMmuUnmap(&pcb->vmSpace->archMmu, uva + (i << PAGE_SHIFT), 1);
            LOS_PhysPageFree(vmPage);
        }
    }

    (VOID)LOS_MuxRelease(&pcb->vmSpace->regionMux);
    return ret;
}

W
wangchenyang 已提交
199
LITE_OS_SEC_TEXT STATIC int LiteIpcMmap(struct file *filep, LosVmMapRegion *region)
W
wenjun 已提交
200 201 202 203
{
    int ret = 0;
    LosVmMapRegion *regionTemp = NULL;
    LosProcessCB *pcb = OsCurrProcessGet();
Z
zhushengle 已提交
204 205 206
    ProcIpcInfo *ipcInfo = pcb->ipcInfo;

    if ((ipcInfo == NULL) || (region == NULL) || (region->range.size > LITE_IPC_POOL_MAX_SIZE) ||
W
wangchenyang 已提交
207
        (!LOS_IsRegionPermUserReadOnly(region)) || (!LOS_IsRegionFlagPrivateOnly(region))) {
W
wenjun 已提交
208 209 210
        ret = -EINVAL;
        goto ERROR_REGION_OUT;
    }
Z
zhushengle 已提交
211
    if (IsPoolMapped(ipcInfo)) {
W
wenjun 已提交
212 213
        return -EEXIST;
    }
Z
zhushengle 已提交
214 215
    if (ipcInfo->pool.uvaddr != NULL) {
        regionTemp = LOS_RegionFind(pcb->vmSpace, (VADDR_T)(UINTPTR)ipcInfo->pool.uvaddr);
W
wenjun 已提交
216 217 218 219
        if (regionTemp != NULL) {
            (VOID)LOS_RegionFree(pcb->vmSpace, regionTemp);
        }
    }
Z
zhushengle 已提交
220 221 222 223
    ipcInfo->pool.uvaddr = (VOID *)(UINTPTR)region->range.base;
    if (ipcInfo->pool.kvaddr != NULL) {
        LOS_VFree(ipcInfo->pool.kvaddr);
        ipcInfo->pool.kvaddr = NULL;
W
wenjun 已提交
224 225
    }
    /* use vmalloc to alloc phy mem */
Z
zhushengle 已提交
226 227
    ipcInfo->pool.kvaddr = LOS_VMalloc(region->range.size);
    if (ipcInfo->pool.kvaddr == NULL) {
W
wenjun 已提交
228 229 230 231 232 233 234 235 236
        ret = -ENOMEM;
        goto ERROR_REGION_OUT;
    }
    /* do mmap */
    ret = DoIpcMmap(pcb, region);
    if (ret) {
        goto ERROR_MAP_OUT;
    }
    /* ipc pool init */
Z
zhushengle 已提交
237
    if (LOS_MemInit(ipcInfo->pool.kvaddr, region->range.size) != LOS_OK) {
W
wenjun 已提交
238 239 240
        ret = -EINVAL;
        goto ERROR_MAP_OUT;
    }
Z
zhushengle 已提交
241
    ipcInfo->pool.poolSize = region->range.size;
W
wenjun 已提交
242 243
    return 0;
ERROR_MAP_OUT:
Z
zhushengle 已提交
244
    LOS_VFree(ipcInfo->pool.kvaddr);
W
wenjun 已提交
245
ERROR_REGION_OUT:
L
lnlan 已提交
246 247 248 249
    if (ipcInfo != NULL) {
        ipcInfo->pool.uvaddr = NULL;
        ipcInfo->pool.kvaddr = NULL;
    }
W
wenjun 已提交
250 251 252
    return ret;
}

Z
zhushengle 已提交
253
LITE_OS_SEC_TEXT_INIT STATIC UINT32 LiteIpcPoolInit(ProcIpcInfo *ipcInfo)
W
wenjun 已提交
254 255 256 257 258
{
    ipcInfo->pool.uvaddr = NULL;
    ipcInfo->pool.kvaddr = NULL;
    ipcInfo->pool.poolSize = 0;
    ipcInfo->ipcTaskID = INVAILD_ID;
Z
zhushengle 已提交
259
    LOS_ListInit(&ipcInfo->ipcUsedNodelist);
W
wenjun 已提交
260 261 262
    return LOS_OK;
}

Z
zhushengle 已提交
263
LITE_OS_SEC_TEXT_INIT STATIC ProcIpcInfo *LiteIpcPoolCreate(VOID)
W
wenjun 已提交
264
{
Z
zhushengle 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
    ProcIpcInfo *ipcInfo = LOS_MemAlloc(m_aucSysMem1, sizeof(ProcIpcInfo));
    if (ipcInfo == NULL) {
        return NULL;
    }

    (VOID)memset_s(ipcInfo, sizeof(ProcIpcInfo), 0, sizeof(ProcIpcInfo));

    (VOID)LiteIpcPoolInit(ipcInfo);
    return ipcInfo;
}

LITE_OS_SEC_TEXT ProcIpcInfo *LiteIpcPoolReInit(const ProcIpcInfo *parent)
{
    ProcIpcInfo *ipcInfo = LiteIpcPoolCreate();
    if (ipcInfo == NULL) {
        return NULL;
    }

    ipcInfo->pool.uvaddr = parent->pool.uvaddr;
    ipcInfo->pool.kvaddr = NULL;
    ipcInfo->pool.poolSize = 0;
    ipcInfo->ipcTaskID = INVAILD_ID;
    return ipcInfo;
W
wenjun 已提交
288 289
}

Z
zhushengle 已提交
290
STATIC VOID LiteIpcPoolDelete(ProcIpcInfo *ipcInfo, UINT32 processID)
W
wenjun 已提交
291 292 293 294 295 296 297
{
    UINT32 intSave;
    IpcUsedNode *node = NULL;
    if (ipcInfo->pool.kvaddr != NULL) {
        LOS_VFree(ipcInfo->pool.kvaddr);
        ipcInfo->pool.kvaddr = NULL;
        IPC_LOCK(intSave);
Z
zhushengle 已提交
298 299
        while (!LOS_ListEmpty(&ipcInfo->ipcUsedNodelist)) {
            node = LOS_DL_LIST_ENTRY(ipcInfo->ipcUsedNodelist.pstNext, IpcUsedNode, list);
W
wenjun 已提交
300 301 302 303 304 305 306 307 308
            LOS_ListDelete(&node->list);
            free(node);
        }
        IPC_UNLOCK(intSave);
    }
    /* remove process access to service */
    for (UINT32 i = 0; i < MAX_SERVICE_NUM; i++) {
        if (ipcInfo->access[i] == TRUE) {
            ipcInfo->access[i] = FALSE;
Z
zhushengle 已提交
309 310 311
            if (OS_TCB_FROM_TID(i)->ipcTaskInfo != NULL) {
                OS_TCB_FROM_TID(i)->ipcTaskInfo->accessMap[processID] = FALSE;
            }
W
wenjun 已提交
312 313 314 315
        }
    }
}

Z
zhushengle 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
LITE_OS_SEC_TEXT UINT32 LiteIpcPoolDestroy(UINT32 processID)
{
    LosProcessCB *pcb = OS_PCB_FROM_PID(processID);
    if (pcb->ipcInfo == NULL) {
        return LOS_NOK;
    }

    LiteIpcPoolDelete(pcb->ipcInfo, pcb->processID);
    LOS_MemFree(m_aucSysMem1, pcb->ipcInfo);
    pcb->ipcInfo = NULL;
    return LOS_OK;
}

LITE_OS_SEC_TEXT_INIT STATIC IpcTaskInfo *LiteIpcTaskInit(VOID)
{
    IpcTaskInfo *taskInfo = LOS_MemAlloc((VOID *)m_aucSysMem1, sizeof(IpcTaskInfo));
    if (taskInfo == NULL) {
        return NULL;
    }

    (VOID)memset_s(taskInfo, sizeof(IpcTaskInfo), 0, sizeof(IpcTaskInfo));
    LOS_ListInit(&taskInfo->msgListHead);
    return taskInfo;
}

W
wenjun 已提交
341
/* Only when kernenl no longer access ipc node content, can user free the ipc node */
Z
zhushengle 已提交
342
LITE_OS_SEC_TEXT STATIC VOID EnableIpcNodeFreeByUser(LosProcessCB *pcb, VOID *buf)
W
wenjun 已提交
343 344
{
    UINT32 intSave;
Z
zhushengle 已提交
345
    ProcIpcInfo *ipcInfo = pcb->ipcInfo;
W
wenjun 已提交
346 347 348 349
    IpcUsedNode *node = (IpcUsedNode *)malloc(sizeof(IpcUsedNode));
    if (node != NULL) {
        node->ptr = buf;
        IPC_LOCK(intSave);
Z
zhushengle 已提交
350
        LOS_ListAdd(&ipcInfo->ipcUsedNodelist, &node->list);
W
wenjun 已提交
351 352 353 354
        IPC_UNLOCK(intSave);
    }
}

Z
zhushengle 已提交
355
LITE_OS_SEC_TEXT STATIC VOID *LiteIpcNodeAlloc(LosProcessCB *pcb, UINT32 size)
W
wenjun 已提交
356
{
Z
zhushengle 已提交
357
    VOID *ptr = LOS_MemAlloc(pcb->ipcInfo->pool.kvaddr, size);
W
wenjun 已提交
358
    PRINT_INFO("LiteIpcNodeAlloc pid:%d, pool:%x buf:%x size:%d\n",
Z
zhushengle 已提交
359
               pcb->processID, pcb->ipcInfo->pool.kvaddr, ptr, size);
W
wenjun 已提交
360 361 362
    return ptr;
}

Z
zhushengle 已提交
363
LITE_OS_SEC_TEXT STATIC UINT32 LiteIpcNodeFree(LosProcessCB *pcb, VOID *buf)
W
wenjun 已提交
364 365
{
    PRINT_INFO("LiteIpcNodeFree pid:%d, pool:%x buf:%x\n",
Z
zhushengle 已提交
366 367
               pcb->processID, pcb->ipcInfo->pool.kvaddr, buf);
    return LOS_MemFree(pcb->ipcInfo->pool.kvaddr, buf);
W
wenjun 已提交
368 369
}

Z
zhushengle 已提交
370
LITE_OS_SEC_TEXT STATIC BOOL IsIpcNode(LosProcessCB *pcb, const VOID *buf)
W
wenjun 已提交
371 372 373
{
    IpcUsedNode *node = NULL;
    UINT32 intSave;
Z
zhushengle 已提交
374
    ProcIpcInfo *ipcInfo = pcb->ipcInfo;
W
wenjun 已提交
375
    IPC_LOCK(intSave);
Z
zhushengle 已提交
376
    LOS_DL_LIST_FOR_EACH_ENTRY(node, &ipcInfo->ipcUsedNodelist, IpcUsedNode, list) {
W
wenjun 已提交
377 378 379 380 381 382 383 384 385 386 387
        if (node->ptr == buf) {
            LOS_ListDelete(&node->list);
            IPC_UNLOCK(intSave);
            free(node);
            return TRUE;
        }
    }
    IPC_UNLOCK(intSave);
    return FALSE;
}

Z
zhushengle 已提交
388
LITE_OS_SEC_TEXT STATIC INTPTR GetIpcUserAddr(const LosProcessCB *pcb, INTPTR kernelAddr)
W
wenjun 已提交
389
{
Z
zhushengle 已提交
390
    IpcPool pool = pcb->ipcInfo->pool;
W
wenjun 已提交
391 392 393 394
    INTPTR offset = (INTPTR)(pool.uvaddr) - (INTPTR)(pool.kvaddr);
    return kernelAddr + offset;
}

Z
zhushengle 已提交
395
LITE_OS_SEC_TEXT STATIC INTPTR GetIpcKernelAddr(const LosProcessCB *pcb, INTPTR userAddr)
W
wenjun 已提交
396
{
Z
zhushengle 已提交
397
    IpcPool pool = pcb->ipcInfo->pool;
W
wenjun 已提交
398 399 400 401 402 403 404 405
    INTPTR offset = (INTPTR)(pool.uvaddr) - (INTPTR)(pool.kvaddr);
    return userAddr - offset;
}

LITE_OS_SEC_TEXT STATIC UINT32 CheckUsedBuffer(const VOID *node, IpcListNode **outPtr)
{
    VOID *ptr = NULL;
    LosProcessCB *pcb = OsCurrProcessGet();
Z
zhushengle 已提交
406
    IpcPool pool = pcb->ipcInfo->pool;
W
wenjun 已提交
407 408 409 410
    if ((node == NULL) || ((INTPTR)node < (INTPTR)(pool.uvaddr)) ||
        ((INTPTR)node > (INTPTR)(pool.uvaddr) + pool.poolSize)) {
        return -EINVAL;
    }
Z
zhushengle 已提交
411 412
    ptr = (VOID *)GetIpcKernelAddr(pcb, (INTPTR)(node));
    if (IsIpcNode(pcb, ptr) != TRUE) {
W
wenjun 已提交
413 414 415 416 417 418 419 420 421 422 423
        return -EFAULT;
    }
    *outPtr = (IpcListNode *)ptr;
    return LOS_OK;
}

LITE_OS_SEC_TEXT STATIC UINT32 GetTid(UINT32 serviceHandle, UINT32 *taskID)
{
    if (serviceHandle >= MAX_SERVICE_NUM) {
        return -EINVAL;
    }
C
Caoruihong 已提交
424
    (VOID)LOS_MuxLock(&g_serviceHandleMapMux, LOS_WAIT_FOREVER);
C
Caoruihong 已提交
425
#if (USE_TASKID_AS_HANDLE == 1)
W
wenjun 已提交
426
    *taskID = serviceHandle ? serviceHandle : g_cmsTask.taskID;
C
Caoruihong 已提交
427
    (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
W
wenjun 已提交
428 429 430 431
    return LOS_OK;
#else
    if (g_serviceHandleMap[serviceHandle].status == HANDLE_REGISTED) {
        *taskID = g_serviceHandleMap[serviceHandle].taskID;
C
Caoruihong 已提交
432
        (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
W
wenjun 已提交
433 434
        return LOS_OK;
    }
C
Caoruihong 已提交
435
    (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
W
wenjun 已提交
436 437 438 439 440 441
    return -EINVAL;
#endif
}

LITE_OS_SEC_TEXT STATIC UINT32 GenerateServiceHandle(UINT32 taskID, HandleStatus status, UINT32 *serviceHandle)
{
C
Caoruihong 已提交
442
    (VOID)LOS_MuxLock(&g_serviceHandleMapMux, LOS_WAIT_FOREVER);
C
Caoruihong 已提交
443
#if (USE_TASKID_AS_HANDLE == 1)
W
wenjun 已提交
444
    *serviceHandle = taskID ? taskID : LOS_CurTaskIDGet(); /* if taskID is 0, return curTaskID */
C
Caoruihong 已提交
445 446 447
    if (*serviceHandle != g_cmsTask.taskID) {
        (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
        return LOS_OK;
W
wenjun 已提交
448 449
    }
#else
C
Caoruihong 已提交
450
    for (UINT32 i = 1; i < MAX_SERVICE_NUM; i++) {
W
wenjun 已提交
451 452 453 454
        if (g_serviceHandleMap[i].status == HANDLE_NOT_USED) {
            g_serviceHandleMap[i].taskID = taskID;
            g_serviceHandleMap[i].status = status;
            *serviceHandle = i;
C
Caoruihong 已提交
455
            (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
W
wenjun 已提交
456 457 458
            return LOS_OK;
        }
    }
C
Caoruihong 已提交
459
#endif
W
wenjun 已提交
460 461 462 463 464 465
    (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
    return -EINVAL;
}

LITE_OS_SEC_TEXT STATIC VOID RefreshServiceHandle(UINT32 serviceHandle, UINT32 result)
{
C
Caoruihong 已提交
466
#if (USE_TASKID_AS_HANDLE == 0)
W
wenjun 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
    (VOID)LOS_MuxLock(&g_serviceHandleMapMux, LOS_WAIT_FOREVER);
    if ((result == LOS_OK) && (g_serviceHandleMap[serviceHandle].status == HANDLE_REGISTING)) {
        g_serviceHandleMap[serviceHandle].status = HANDLE_REGISTED;
    } else {
        g_serviceHandleMap[serviceHandle].status = HANDLE_NOT_USED;
    }
    (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
#endif
}

LITE_OS_SEC_TEXT STATIC UINT32 AddServiceAccess(UINT32 taskID, UINT32 serviceHandle)
{
    UINT32 serviceTid = 0;
    UINT32 ret = GetTid(serviceHandle, &serviceTid);
    if (ret != LOS_OK) {
Z
zhushengle 已提交
482
        PRINT_ERR("Liteipc AddServiceAccess GetTid failed\n");
W
wenjun 已提交
483 484
        return ret;
    }
485

Z
zhushengle 已提交
486
    LosTaskCB *tcb = OS_TCB_FROM_TID(serviceTid);
Z
zhushengle 已提交
487
    LosProcessCB *pcb = OS_PCB_FROM_TID(taskID);
Z
zhushengle 已提交
488
    if ((tcb->ipcTaskInfo == NULL) || (pcb->ipcInfo == NULL)) {
Z
zhushengle 已提交
489
        PRINT_ERR("Liteipc AddServiceAccess ipc not create! pid %u tid %u\n", pcb->processID, tcb->taskID);
Z
zhushengle 已提交
490 491
        return -EINVAL;
    }
Z
zhushengle 已提交
492
    tcb->ipcTaskInfo->accessMap[pcb->processID] = TRUE;
Z
zhushengle 已提交
493
    pcb->ipcInfo->access[serviceTid] = TRUE;
W
wenjun 已提交
494 495 496 497 498 499
    return LOS_OK;
}

LITE_OS_SEC_TEXT STATIC BOOL HasServiceAccess(UINT32 serviceHandle)
{
    UINT32 serviceTid = 0;
Z
zhushengle 已提交
500
    LosProcessCB *curr = OsCurrProcessGet();
W
wenjun 已提交
501 502 503 504 505 506 507 508 509
    UINT32 ret;
    if (serviceHandle >= MAX_SERVICE_NUM) {
        return FALSE;
    }
    if (serviceHandle == 0) {
        return TRUE;
    }
    ret = GetTid(serviceHandle, &serviceTid);
    if (ret != LOS_OK) {
Z
zhushengle 已提交
510
        PRINT_ERR("Liteipc HasServiceAccess GetTid failed\n");
W
wenjun 已提交
511 512
        return FALSE;
    }
Z
zhushengle 已提交
513 514
    LosTaskCB *taskCB = OS_TCB_FROM_TID(serviceTid);
    if (taskCB->processCB == (UINTPTR)curr) {
W
wenjun 已提交
515 516
        return TRUE;
    }
Z
zhushengle 已提交
517

Z
zhushengle 已提交
518
    if (taskCB->ipcTaskInfo == NULL) {
Z
zhushengle 已提交
519 520 521
        return FALSE;
    }

Z
zhushengle 已提交
522
    return taskCB->ipcTaskInfo->accessMap[curr->processID];
W
wenjun 已提交
523 524 525 526
}

LITE_OS_SEC_TEXT STATIC UINT32 SetIpcTask(VOID)
{
Z
zhushengle 已提交
527 528 529
    if (OsCurrProcessGet()->ipcInfo->ipcTaskID == INVAILD_ID) {
        OsCurrProcessGet()->ipcInfo->ipcTaskID = LOS_CurTaskIDGet();
        return OsCurrProcessGet()->ipcInfo->ipcTaskID;
W
wenjun 已提交
530
    }
Z
zhushengle 已提交
531
    PRINT_ERR("Liteipc curprocess %d IpcTask already set!\n", OsCurrProcessGet()->processID);
W
wenjun 已提交
532 533 534 535 536
    return -EINVAL;
}

LITE_OS_SEC_TEXT BOOL IsIpcTaskSet(VOID)
{
Z
zhushengle 已提交
537
    if (OsCurrProcessGet()->ipcInfo->ipcTaskID == INVAILD_ID) {
W
wenjun 已提交
538 539 540 541 542
        return FALSE;
    }
    return TRUE;
}

Z
zhushengle 已提交
543
LITE_OS_SEC_TEXT STATIC UINT32 GetIpcTaskID(LosProcessCB *pcb, UINT32 *ipcTaskID)
W
wenjun 已提交
544
{
Z
zhushengle 已提交
545
    if (pcb->ipcInfo->ipcTaskID == INVAILD_ID) {
W
wenjun 已提交
546 547
        return LOS_NOK;
    }
Z
zhushengle 已提交
548
    *ipcTaskID = pcb->ipcInfo->ipcTaskID;
W
wenjun 已提交
549 550 551 552 553 554 555 556 557
    return LOS_OK;
}

LITE_OS_SEC_TEXT STATIC UINT32 SendDeathMsg(UINT32 processID, UINT32 serviceHandle)
{
    UINT32 ipcTaskID;
    UINT32 ret;
    IpcContent content;
    IpcMsg msg;
Z
zhushengle 已提交
558 559 560 561 562
    LosProcessCB *pcb = OS_PCB_FROM_PID(processID);

    if (pcb->ipcInfo == NULL) {
        return -EINVAL;
    }
W
wenjun 已提交
563

Z
zhushengle 已提交
564
    pcb->ipcInfo->access[serviceHandle] = FALSE;
W
wenjun 已提交
565

Z
zhushengle 已提交
566
    ret = GetIpcTaskID(pcb, &ipcTaskID);
W
wenjun 已提交
567 568 569 570 571
    if (ret != LOS_OK) {
        return -EINVAL;
    }
    content.flag = SEND;
    content.outMsg = &msg;
A
arvinzzz 已提交
572
    (void)memset_s(content.outMsg, sizeof(IpcMsg), 0, sizeof(IpcMsg));
W
wenjun 已提交
573 574 575 576 577 578 579
    content.outMsg->type = MT_DEATH_NOTIFY;
    content.outMsg->target.handle = ipcTaskID;
    content.outMsg->target.token = serviceHandle;
    content.outMsg->code = 0;
    return LiteIpcWrite(&content);
}

Z
zhushengle 已提交
580
LITE_OS_SEC_TEXT VOID LiteIpcRemoveServiceHandle(UINT32 taskID)
W
wenjun 已提交
581 582
{
    UINT32 j;
Z
zhushengle 已提交
583 584 585 586 587 588
    LosTaskCB *taskCB = OS_TCB_FROM_TID(taskID);
    IpcTaskInfo *ipcTaskInfo = taskCB->ipcTaskInfo;
    if (ipcTaskInfo == NULL) {
        return;
    }

C
Caoruihong 已提交
589
#if (USE_TASKID_AS_HANDLE == 1)
W
wenjun 已提交
590 591 592 593 594

    UINT32 intSave;
    LOS_DL_LIST *listHead = NULL;
    LOS_DL_LIST *listNode = NULL;
    IpcListNode *node = NULL;
Z
zhushengle 已提交
595
    LosProcessCB *pcb = OS_PCB_FROM_TCB(taskCB);
W
wenjun 已提交
596

Z
zhushengle 已提交
597
    listHead = &(ipcTaskInfo->msgListHead);
W
wenjun 已提交
598 599 600 601 602 603 604 605 606 607 608
    do {
        SCHEDULER_LOCK(intSave);
        if (LOS_ListEmpty(listHead)) {
            SCHEDULER_UNLOCK(intSave);
            break;
        } else {
            listNode = LOS_DL_LIST_FIRST(listHead);
            LOS_ListDelete(listNode);
            node = LOS_DL_LIST_ENTRY(listNode, IpcListNode, listNode);
            SCHEDULER_UNLOCK(intSave);
            (VOID)HandleSpecialObjects(taskCB->taskID, node, TRUE);
Z
zhushengle 已提交
609
            (VOID)LiteIpcNodeFree(pcb, (VOID *)node);
W
wenjun 已提交
610 611 612
        }
    } while (1);

Z
zhushengle 已提交
613 614
    ipcTaskInfo->accessMap[pcb->processID] = FALSE;
    for (j = 0; j < LOSCFG_BASE_CORE_PROCESS_LIMIT; j++) {
Z
zhushengle 已提交
615 616
        if (ipcTaskInfo->accessMap[j] == TRUE) {
            ipcTaskInfo->accessMap[j] = FALSE;
W
wenjun 已提交
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
            (VOID)SendDeathMsg(j, taskCB->taskID);
        }
    }
#else
    (VOID)LOS_MuxLock(&g_serviceHandleMapMux, LOS_WAIT_FOREVER);
    for (UINT32 i = 1; i < MAX_SERVICE_NUM; i++) {
        if ((g_serviceHandleMap[i].status != HANDLE_NOT_USED) && (g_serviceHandleMap[i].taskID == taskCB->taskID)) {
            g_serviceHandleMap[i].status = HANDLE_NOT_USED;
            g_serviceHandleMap[i].taskID = INVAILD_ID;
            break;
        }
    }
    (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
    /* run deathHandler */
    if (i < MAX_SERVICE_NUM) {
Z
zhushengle 已提交
632
        for (j = 0; j < LOSCFG_BASE_CORE_PROCESS_LIMIT; j++) {
Z
zhushengle 已提交
633
            if (ipcTaskInfo->accessMap[j] == TRUE) {
W
wenjun 已提交
634 635 636 637 638
                (VOID)SendDeathMsg(j, i);
            }
        }
    }
#endif
Z
zhushengle 已提交
639 640 641

    (VOID)LOS_MemFree(m_aucSysMem1, ipcTaskInfo);
    taskCB->ipcTaskInfo = NULL;
W
wenjun 已提交
642 643 644 645 646 647 648 649
}

LITE_OS_SEC_TEXT STATIC UINT32 SetCms(UINTPTR maxMsgSize)
{
    if (maxMsgSize < sizeof(IpcMsg)) {
        return -EINVAL;
    }
    (VOID)LOS_MuxLock(&g_serviceHandleMapMux, LOS_WAIT_FOREVER);
C
Caoruihong 已提交
650
#if (USE_TASKID_AS_HANDLE == 1)
W
wenjun 已提交
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
    if (g_cmsTask.status == HANDLE_NOT_USED) {
        g_cmsTask.status = HANDLE_REGISTED;
        g_cmsTask.taskID = LOS_CurTaskIDGet();
        g_cmsTask.maxMsgSize = maxMsgSize;
        (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
        return LOS_OK;
    }
#else
    if (g_serviceHandleMap[0].status == HANDLE_NOT_USED) {
        g_serviceHandleMap[0].status = HANDLE_REGISTED;
        g_serviceHandleMap[0].taskID = LOS_CurTaskIDGet();
        (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
        return LOS_OK;
    }
#endif
    (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
    return -EEXIST;
}

LITE_OS_SEC_TEXT STATIC BOOL IsCmsSet(VOID)
{
C
Caoruihong 已提交
672 673
    BOOL ret;
    (VOID)LOS_MuxLock(&g_serviceHandleMapMux, LOS_WAIT_FOREVER);
C
Caoruihong 已提交
674
#if (USE_TASKID_AS_HANDLE == 1)
C
Caoruihong 已提交
675
    ret = g_cmsTask.status == HANDLE_REGISTED;
W
wenjun 已提交
676
#else
C
Caoruihong 已提交
677
    ret = g_serviceHandleMap[0].status == HANDLE_REGISTED;
W
wenjun 已提交
678
#endif
C
Caoruihong 已提交
679 680
    (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
    return ret;
W
wenjun 已提交
681 682 683 684
}

LITE_OS_SEC_TEXT STATIC BOOL IsCmsTask(UINT32 taskID)
{
C
Caoruihong 已提交
685 686
    BOOL ret;
    (VOID)LOS_MuxLock(&g_serviceHandleMapMux, LOS_WAIT_FOREVER);
C
Caoruihong 已提交
687
#if (USE_TASKID_AS_HANDLE == 1)
Z
zhushengle 已提交
688
    ret = IsCmsSet() ? (OS_TCB_FROM_TID(taskID)->processCB == OS_TCB_FROM_TID(g_cmsTask.taskID)->processCB) : FALSE;
W
wenjun 已提交
689
#else
Z
zhushengle 已提交
690 691
    ret = IsCmsSet() ? (OS_TCB_FROM_TID(taskID)->processCB ==
        OS_TCB_FROM_TID(g_serviceHandleMap[0].taskID)->processCB) : FALSE;
W
wenjun 已提交
692
#endif
C
Caoruihong 已提交
693 694
    (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
    return ret;
W
wenjun 已提交
695 696 697 698 699 700 701 702 703
}

LITE_OS_SEC_TEXT STATIC BOOL IsTaskAlive(UINT32 taskID)
{
    LosTaskCB *tcb = NULL;
    if (OS_TID_CHECK_INVALID(taskID)) {
        return FALSE;
    }
    tcb = OS_TCB_FROM_TID(taskID);
704
    if (OsTaskIsUnused(tcb)) {
W
wenjun 已提交
705 706 707 708 709
        return FALSE;
    }
    if (OsTaskIsInactive(tcb)) {
        return FALSE;
    }
Z
zhushengle 已提交
710 711 712
    if (!OsTaskIsUserMode(tcb)) {
        return FALSE;
    }
W
wenjun 已提交
713 714 715
    return TRUE;
}

Z
zhushengle 已提交
716
LITE_OS_SEC_TEXT STATIC UINT32 HandleFd(const LosProcessCB *pcb, SpecialObj *obj, BOOL isRollback)
W
wenjun 已提交
717
{
Z
zhOu 已提交
718 719
    int ret;
    if (isRollback == FALSE) {
Z
zhushengle 已提交
720
        ret = CopyFdToProc(obj->content.fd, pcb->processID);
Z
zhOu 已提交
721 722 723 724 725
        if (ret < 0) {
            return ret;
        }
        obj->content.fd = ret;
    } else {
Z
zhushengle 已提交
726
        ret = CloseProcFd(obj->content.fd, pcb->processID);
Z
zhOu 已提交
727 728 729 730 731
        if (ret < 0) {
            return ret;
        }
    }

W
wenjun 已提交
732 733 734
    return LOS_OK;
}

Z
zhushengle 已提交
735
LITE_OS_SEC_TEXT STATIC UINT32 HandlePtr(LosProcessCB *pcb, SpecialObj *obj, BOOL isRollback)
W
wenjun 已提交
736 737 738 739 740 741 742 743
{
    VOID *buf = NULL;
    UINT32 ret;
    if ((obj->content.ptr.buff == NULL) || (obj->content.ptr.buffSz == 0)) {
        return -EINVAL;
    }
    if (isRollback == FALSE) {
        if (LOS_IsUserAddress((vaddr_t)(UINTPTR)(obj->content.ptr.buff)) == FALSE) {
Z
zhushengle 已提交
744
            PRINT_ERR("Liteipc Bad ptr address\n");
W
wenjun 已提交
745 746
            return -EINVAL;
        }
Z
zhushengle 已提交
747
        buf = LiteIpcNodeAlloc(pcb, obj->content.ptr.buffSz);
W
wenjun 已提交
748
        if (buf == NULL) {
Z
zhushengle 已提交
749
            PRINT_ERR("Liteipc DealPtr alloc mem failed\n");
W
wenjun 已提交
750 751 752 753
            return -EINVAL;
        }
        ret = copy_from_user(buf, obj->content.ptr.buff, obj->content.ptr.buffSz);
        if (ret != LOS_OK) {
Z
zhushengle 已提交
754
            LiteIpcNodeFree(pcb, buf);
W
wenjun 已提交
755 756
            return ret;
        }
Z
zhushengle 已提交
757 758
        obj->content.ptr.buff = (VOID *)GetIpcUserAddr(pcb, (INTPTR)buf);
        EnableIpcNodeFreeByUser(pcb, (VOID *)buf);
W
wenjun 已提交
759
    } else {
Z
zhushengle 已提交
760
        (VOID)LiteIpcNodeFree(pcb, (VOID *)GetIpcKernelAddr(pcb, (INTPTR)obj->content.ptr.buff));
W
wenjun 已提交
761 762 763 764
    }
    return LOS_OK;
}

765
LITE_OS_SEC_TEXT STATIC UINT32 HandleSvc(UINT32 dstTid, SpecialObj *obj, BOOL isRollback)
W
wenjun 已提交
766 767 768
{
    UINT32 taskID = 0;
    if (isRollback == FALSE) {
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
        if (obj->content.svc.handle == -1) {
            if (obj->content.svc.token != 1) {
                PRINT_ERR("Liteipc HandleSvc wrong svc token\n");
                return -EINVAL;
            }
            UINT32 selfTid = LOS_CurTaskIDGet();
            LosTaskCB *tcb = OS_TCB_FROM_TID(selfTid);
            if (tcb->ipcTaskInfo == NULL) {
                tcb->ipcTaskInfo = LiteIpcTaskInit();
            }
            uint32_t serviceHandle = 0;
            UINT32 ret = GenerateServiceHandle(selfTid, HANDLE_REGISTED, &serviceHandle);
            if (ret != LOS_OK) {
                PRINT_ERR("Liteipc GenerateServiceHandle failed.\n");
                return ret;
            }
            obj->content.svc.handle = serviceHandle;
            (VOID)LOS_MuxLock(&g_serviceHandleMapMux, LOS_WAIT_FOREVER);
            AddServiceAccess(dstTid, serviceHandle);
            (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
        }
W
wenjun 已提交
790
        if (IsTaskAlive(obj->content.svc.handle) == FALSE) {
Z
zhushengle 已提交
791
            PRINT_ERR("Liteipc HandleSvc wrong svctid\n");
W
wenjun 已提交
792 793 794
            return -EINVAL;
        }
        if (HasServiceAccess(obj->content.svc.handle) == FALSE) {
795
            PRINT_ERR("Liteipc %s, %d, svchandle:%d, tid:%d\n", __FUNCTION__, __LINE__, obj->content.svc.handle, LOS_CurTaskIDGet());
W
wenjun 已提交
796 797
            return -EACCES;
        }
798 799 800 801
        LosTaskCB *taskCb = OS_TCB_FROM_TID(obj->content.svc.handle);
        if (taskCb->ipcTaskInfo == NULL) {
            taskCb->ipcTaskInfo = LiteIpcTaskInit();
        }
W
wenjun 已提交
802
        if (GetTid(obj->content.svc.handle, &taskID) == 0) {
803
            AddServiceAccess(dstTid, obj->content.svc.handle);
W
wenjun 已提交
804 805 806 807 808 809 810 811
        }
    }
    return LOS_OK;
}

LITE_OS_SEC_TEXT STATIC UINT32 HandleObj(UINT32 dstTid, SpecialObj *obj, BOOL isRollback)
{
    UINT32 ret;
Z
zhushengle 已提交
812
    LosProcessCB *pcb = OS_PCB_FROM_TID(dstTid);
W
wenjun 已提交
813 814
    switch (obj->type) {
        case OBJ_FD:
Z
zhushengle 已提交
815
            ret = HandleFd(pcb, obj, isRollback);
W
wenjun 已提交
816 817
            break;
        case OBJ_PTR:
Z
zhushengle 已提交
818
            ret = HandlePtr(pcb, obj, isRollback);
W
wenjun 已提交
819 820
            break;
        case OBJ_SVC:
821
            ret = HandleSvc(dstTid, (SpecialObj *)obj, isRollback);
W
wenjun 已提交
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
            break;
        default:
            ret = -EINVAL;
            break;
    }
    return ret;
}

LITE_OS_SEC_TEXT STATIC UINT32 HandleSpecialObjects(UINT32 dstTid, IpcListNode *node, BOOL isRollback)
{
    UINT32 ret = LOS_OK;
    IpcMsg *msg = &(node->msg);
    INT32 i;
    SpecialObj *obj = NULL;
    UINT32 *offset = (UINT32 *)(UINTPTR)(msg->offsets);
    if (isRollback) {
        i = msg->spObjNum;
        goto EXIT;
    }
    for (i = 0; i < msg->spObjNum; i++) {
        if (offset[i] > msg->dataSz - sizeof(SpecialObj)) {
            ret = -EINVAL;
            goto EXIT;
        }
        if ((i > 0) && (offset[i] < offset[i - 1] + sizeof(SpecialObj))) {
            ret = -EINVAL;
            goto EXIT;
        }
        obj = (SpecialObj *)((UINTPTR)msg->data + offset[i]);
        if (obj == NULL) {
            ret = -EINVAL;
            goto EXIT;
        }
        ret = HandleObj(dstTid, obj, FALSE);
        if (ret != LOS_OK) {
            goto EXIT;
        }
    }
    return LOS_OK;
EXIT:
    for (i--; i >= 0; i--) {
        obj = (SpecialObj *)((UINTPTR)msg->data + offset[i]);
        (VOID)HandleObj(dstTid, obj, TRUE);
    }
    return ret;
}

LITE_OS_SEC_TEXT STATIC UINT32 CheckMsgSize(IpcMsg *msg)
{
    UINT64 totalSize;
    UINT32 i;
    UINT32 *offset = (UINT32 *)(UINTPTR)(msg->offsets);
    SpecialObj *obj = NULL;
    if (msg->target.handle != 0) {
        return LOS_OK;
    }
    /* msg send to cms, check the msg size */
    totalSize = (UINT64)sizeof(IpcMsg) + msg->dataSz + msg->spObjNum * sizeof(UINT32);
    for (i = 0; i < msg->spObjNum; i++) {
        if (offset[i] > msg->dataSz - sizeof(SpecialObj)) {
            return -EINVAL;
        }
        if ((i > 0) && (offset[i] < offset[i - 1] + sizeof(SpecialObj))) {
            return -EINVAL;
        }
        obj = (SpecialObj *)((UINTPTR)msg->data + offset[i]);
        if (obj == NULL) {
            return -EINVAL;
        }
        if (obj->type == OBJ_PTR) {
            totalSize += obj->content.ptr.buffSz;
        }
    }
895
    (VOID)LOS_MuxLock(&g_serviceHandleMapMux, LOS_WAIT_FOREVER);
W
wenjun 已提交
896
    if (totalSize > g_cmsTask.maxMsgSize) {
C
Caoruihong 已提交
897
        (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
W
wenjun 已提交
898 899
        return -EINVAL;
    }
C
Caoruihong 已提交
900
    (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
W
wenjun 已提交
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
    return LOS_OK;
}

LITE_OS_SEC_TEXT STATIC UINT32 CopyDataFromUser(IpcListNode *node, UINT32 bufSz, const IpcMsg *msg)
{
    UINT32 ret;
    ret = (UINT32)memcpy_s((VOID *)(&node->msg), bufSz - sizeof(LOS_DL_LIST), (const VOID *)msg, sizeof(IpcMsg));
    if (ret != LOS_OK) {
        PRINT_DEBUG("%s, %d, %u\n", __FUNCTION__, __LINE__, ret);
        return ret;
    }

    if (msg->dataSz) {
        node->msg.data = (VOID *)((UINTPTR)node + sizeof(IpcListNode));
        ret = copy_from_user((VOID *)(node->msg.data), msg->data, msg->dataSz);
        if (ret != LOS_OK) {
            PRINT_DEBUG("%s, %d\n", __FUNCTION__, __LINE__);
            return ret;
        }
    } else {
        node->msg.data = NULL;
    }

    if (msg->spObjNum) {
        node->msg.offsets = (VOID *)((UINTPTR)node + sizeof(IpcListNode) + msg->dataSz);
        ret = copy_from_user((VOID *)(node->msg.offsets), msg->offsets, msg->spObjNum * sizeof(UINT32));
        if (ret != LOS_OK) {
            PRINT_DEBUG("%s, %d, %x, %x, %d\n", __FUNCTION__, __LINE__, node->msg.offsets, msg->offsets, msg->spObjNum);
            return ret;
        }
    } else {
        node->msg.offsets = NULL;
    }
    ret = CheckMsgSize(&node->msg);
    if (ret != LOS_OK) {
        PRINT_DEBUG("%s, %d\n", __FUNCTION__, __LINE__);
        return ret;
    }
    node->msg.taskID = LOS_CurTaskIDGet();
    node->msg.processID = OsCurrProcessGet()->processID;
#ifdef LOSCFG_SECURITY_CAPABILITY
    node->msg.userID = OsCurrProcessGet()->user->userID;
    node->msg.gid = OsCurrProcessGet()->user->gid;
#endif
    return LOS_OK;
}

948
LITE_OS_SEC_TEXT STATIC BOOL IsValidReply(const IpcContent *content)
W
wenjun 已提交
949
{
Z
zhushengle 已提交
950 951
    LosProcessCB *curr = OsCurrProcessGet();
    IpcListNode *node = (IpcListNode *)GetIpcKernelAddr(curr, (INTPTR)(content->buffToFree));
W
wenjun 已提交
952 953 954 955 956 957 958 959 960
    IpcMsg *requestMsg = &node->msg;
    IpcMsg *replyMsg = content->outMsg;
    UINT32 reqDstTid = 0;
    /* Check whether the reply matches the request */
    if ((requestMsg->type != MT_REQUEST)  ||
        (requestMsg->flag == LITEIPC_FLAG_ONEWAY) ||
        (replyMsg->timestamp != requestMsg->timestamp) ||
        (replyMsg->target.handle != requestMsg->taskID) ||
        (GetTid(requestMsg->target.handle, &reqDstTid) != 0) ||
Z
zhushengle 已提交
961
        (OS_TCB_FROM_TID(reqDstTid)->processCB != (UINTPTR)curr)) {
W
wenjun 已提交
962 963 964 965 966 967 968 969 970 971
        return FALSE;
    }
    return TRUE;
}

LITE_OS_SEC_TEXT STATIC UINT32 CheckPara(IpcContent *content, UINT32 *dstTid)
{
    UINT32 ret;
    IpcMsg *msg = content->outMsg;
    UINT32 flag = content->flag;
C
Caoruihong 已提交
972
#if (USE_TIMESTAMP == 1)
W
wenjun 已提交
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
    UINT64 now = LOS_CurrNanosec();
#endif
    if (((msg->dataSz > 0) && (msg->data == NULL)) ||
        ((msg->spObjNum > 0) && (msg->offsets == NULL)) ||
        (msg->dataSz > IPC_MSG_DATA_SZ_MAX) ||
        (msg->spObjNum > IPC_MSG_OBJECT_NUM_MAX) ||
        (msg->dataSz < msg->spObjNum * sizeof(SpecialObj))) {
        return -EINVAL;
    }
    switch (msg->type) {
        case MT_REQUEST:
            if (HasServiceAccess(msg->target.handle)) {
                ret = GetTid(msg->target.handle, dstTid);
                if (ret != LOS_OK) {
                    return -EINVAL;
                }
            } else {
Z
zhushengle 已提交
990
                PRINT_ERR("Liteipc %s, %d\n", __FUNCTION__, __LINE__);
W
wenjun 已提交
991 992
                return -EACCES;
            }
C
Caoruihong 已提交
993
#if (USE_TIMESTAMP == 1)
W
wenjun 已提交
994 995 996 997 998 999 1000 1001
            msg->timestamp = now;
#endif
            break;
        case MT_REPLY:
        case MT_FAILED_REPLY:
            if ((flag & BUFF_FREE) != BUFF_FREE) {
                return -EINVAL;
            }
1002
            if (!IsValidReply(content)) {
W
wenjun 已提交
1003 1004
                return -EINVAL;
            }
C
Caoruihong 已提交
1005
#if (USE_TIMESTAMP == 1)
W
wenjun 已提交
1006
            if (now > msg->timestamp + LITEIPC_TIMEOUT_NS) {
L
LiteOS2021 已提交
1007 1008 1009 1010 1011
#ifdef LOSCFG_KERNEL_HOOK
                ret = GetTid(msg->target.handle, dstTid);
                if (ret != LOS_OK) {
                    *dstTid = INVAILD_ID;
                }
W
wenjun 已提交
1012
#endif
L
LiteOS2021 已提交
1013
                OsHookCall(LOS_HOOK_TYPE_IPC_WRITE_DROP, msg, *dstTid,
Z
zhushengle 已提交
1014
                           (*dstTid == INVAILD_ID) ? INVAILD_ID : OS_PCB_FROM_TID(*dstTid)->processID, 0);
Z
zhushengle 已提交
1015
                PRINT_ERR("Liteipc A timeout reply, request timestamp:%lld, now:%lld\n", msg->timestamp, now);
W
wenjun 已提交
1016 1017 1018 1019 1020 1021 1022
                return -ETIME;
            }
#endif
            *dstTid = msg->target.handle;
            break;
        case MT_DEATH_NOTIFY:
            *dstTid = msg->target.handle;
C
Caoruihong 已提交
1023
#if (USE_TIMESTAMP == 1)
W
wenjun 已提交
1024 1025 1026 1027
            msg->timestamp = now;
#endif
            break;
        default:
L
lihongjin 已提交
1028
            PRINT_DEBUG("Unknown msg type:%d\n", msg->type);
W
wenjun 已提交
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
            return -EINVAL;
    }

    if (IsTaskAlive(*dstTid) == FALSE) {
        return -EINVAL;
    }
    return LOS_OK;
}

LITE_OS_SEC_TEXT STATIC UINT32 LiteIpcWrite(IpcContent *content)
{
    UINT32 ret, intSave;
    UINT32 dstTid;

    IpcMsg *msg = content->outMsg;

    ret = CheckPara(content, &dstTid);
    if (ret != LOS_OK) {
        return ret;
    }

Z
zhushengle 已提交
1050
    LosTaskCB *tcb = OS_TCB_FROM_TID(dstTid);
Z
zhushengle 已提交
1051
    LosProcessCB *pcb = OS_PCB_FROM_TCB(tcb);
Z
zhushengle 已提交
1052
    if (pcb->ipcInfo == NULL) {
Z
zhushengle 已提交
1053
        PRINT_ERR("pid %u Liteipc not create\n", pcb->processID);
Z
zhushengle 已提交
1054 1055 1056
        return -EINVAL;
    }

W
wenjun 已提交
1057
    UINT32 bufSz = sizeof(IpcListNode) + msg->dataSz + msg->spObjNum * sizeof(UINT32);
Z
zhushengle 已提交
1058
    IpcListNode *buf = (IpcListNode *)LiteIpcNodeAlloc(pcb, bufSz);
W
wenjun 已提交
1059 1060 1061 1062 1063 1064 1065 1066 1067
    if (buf == NULL) {
        PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
        return -ENOMEM;
    }
    ret = CopyDataFromUser(buf, bufSz, (const IpcMsg *)msg);
    if (ret != LOS_OK) {
        PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
        goto ERROR_COPY;
    }
Z
zhushengle 已提交
1068 1069 1070 1071 1072

    if (tcb->ipcTaskInfo == NULL) {
        tcb->ipcTaskInfo = LiteIpcTaskInit();
    }

W
wenjun 已提交
1073 1074 1075 1076 1077 1078 1079
    ret = HandleSpecialObjects(dstTid, buf, FALSE);
    if (ret != LOS_OK) {
        PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
        goto ERROR_COPY;
    }
    /* add data to list and wake up dest task */
    SCHEDULER_LOCK(intSave);
Z
zhushengle 已提交
1080
    LOS_ListTailInsert(&(tcb->ipcTaskInfo->msgListHead), &(buf->listNode));
Z
zhushengle 已提交
1081
    OsHookCall(LOS_HOOK_TYPE_IPC_WRITE, &buf->msg, dstTid, pcb->processID, tcb->waitFlag);
1082
    if (tcb->waitFlag == OS_TASK_WAIT_LITEIPC) {
M
mamingshuai 已提交
1083
        OsTaskWakeClearPendMask(tcb);
Z
zhushengle 已提交
1084
        tcb->ops->wake(tcb);
W
wenjun 已提交
1085 1086 1087 1088 1089 1090 1091 1092
        SCHEDULER_UNLOCK(intSave);
        LOS_MpSchedule(OS_MP_CPU_ALL);
        LOS_Schedule();
    } else {
        SCHEDULER_UNLOCK(intSave);
    }
    return LOS_OK;
ERROR_COPY:
Z
zhushengle 已提交
1093
    LiteIpcNodeFree(pcb, buf);
W
wenjun 已提交
1094 1095 1096
    return ret;
}

1097
LITE_OS_SEC_TEXT STATIC UINT32 CheckReceivedMsg(IpcListNode *node, IpcContent *content, LosTaskCB *tcb)
W
wenjun 已提交
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
{
    UINT32 ret = LOS_OK;
    if (node == NULL) {
        return -EINVAL;
    }
    switch (node->msg.type) {
        case MT_REQUEST:
            if ((content->flag & SEND) == SEND) {
                PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
                ret = -EINVAL;
            }
            break;
        case MT_FAILED_REPLY:
            ret = -ENOENT;
            /* fall-through */
        case MT_REPLY:
            if ((content->flag & SEND) != SEND) {
                PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
                ret = -EINVAL;
            }
C
Caoruihong 已提交
1118
#if (USE_TIMESTAMP == 1)
W
wenjun 已提交
1119
            if (node->msg.timestamp != content->outMsg->timestamp) {
L
lihongjin 已提交
1120
                PRINT_ERR("Receive a unmatch reply, drop it\n");
W
wenjun 已提交
1121 1122 1123 1124 1125
                ret = -EINVAL;
            }
#else
            if ((node->msg.code != content->outMsg->code) ||
                (node->msg.target.token != content->outMsg->target.token)) {
L
lihongjin 已提交
1126
                PRINT_ERR("Receive a unmatch reply, drop it\n");
W
wenjun 已提交
1127 1128 1129 1130 1131 1132 1133
                ret = -EINVAL;
            }
#endif
            break;
        case MT_DEATH_NOTIFY:
            break;
        default:
L
lihongjin 已提交
1134
            PRINT_ERR("Unknown msg type:%d\n", node->msg.type);
W
wenjun 已提交
1135 1136 1137
            ret =  -EINVAL;
    }
    if (ret != LOS_OK) {
1138
        OsHookCall(LOS_HOOK_TYPE_IPC_READ_DROP, &node->msg, tcb->waitFlag);
W
wenjun 已提交
1139
        (VOID)HandleSpecialObjects(LOS_CurTaskIDGet(), node, TRUE);
Z
zhushengle 已提交
1140
        (VOID)LiteIpcNodeFree(OsCurrProcessGet(), (VOID *)node);
W
wenjun 已提交
1141
    } else {
1142
        OsHookCall(LOS_HOOK_TYPE_IPC_READ, &node->msg, tcb->waitFlag);
W
wenjun 已提交
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
    }
    return ret;
}

LITE_OS_SEC_TEXT STATIC UINT32 LiteIpcRead(IpcContent *content)
{
    UINT32 intSave, ret;
    UINT32 selfTid = LOS_CurTaskIDGet();
    LOS_DL_LIST *listHead = NULL;
    LOS_DL_LIST *listNode = NULL;
    IpcListNode *node = NULL;
    UINT32 syncFlag = (content->flag & SEND) && (content->flag & RECV);
    UINT32 timeout = syncFlag ? LOS_MS2Tick(LITEIPC_TIMEOUT_MS) : LOS_WAIT_FOREVER;

    LosTaskCB *tcb = OS_TCB_FROM_TID(selfTid);
Z
zhushengle 已提交
1158 1159 1160 1161 1162
    if (tcb->ipcTaskInfo == NULL) {
        tcb->ipcTaskInfo = LiteIpcTaskInit();
    }

    listHead = &(tcb->ipcTaskInfo->msgListHead);
W
wenjun 已提交
1163 1164 1165
    do {
        SCHEDULER_LOCK(intSave);
        if (LOS_ListEmpty(listHead)) {
M
mamingshuai 已提交
1166
            OsTaskWaitSetPendMask(OS_TASK_WAIT_LITEIPC, OS_INVALID_VALUE, timeout);
1167
            OsHookCall(LOS_HOOK_TYPE_IPC_TRY_READ, syncFlag ? MT_REPLY : MT_REQUEST, tcb->waitFlag);
Z
zhushengle 已提交
1168
            ret = tcb->ops->wait(tcb, &g_ipcPendlist, timeout);
W
wenjun 已提交
1169
            if (ret == LOS_ERRNO_TSK_TIMEOUT) {
1170
                OsHookCall(LOS_HOOK_TYPE_IPC_READ_TIMEOUT, syncFlag ? MT_REPLY : MT_REQUEST, tcb->waitFlag);
W
wenjun 已提交
1171 1172 1173 1174
                SCHEDULER_UNLOCK(intSave);
                return -ETIME;
            }

1175
            if (OsTaskIsKilled(tcb)) {
1176
                OsHookCall(LOS_HOOK_TYPE_IPC_KILL, syncFlag ? MT_REPLY : MT_REQUEST, tcb->waitFlag);
1177 1178 1179 1180
                SCHEDULER_UNLOCK(intSave);
                return -ERFKILL;
            }

W
wenjun 已提交
1181 1182 1183 1184 1185 1186
            SCHEDULER_UNLOCK(intSave);
        } else {
            listNode = LOS_DL_LIST_FIRST(listHead);
            LOS_ListDelete(listNode);
            node = LOS_DL_LIST_ENTRY(listNode, IpcListNode, listNode);
            SCHEDULER_UNLOCK(intSave);
1187
            ret = CheckReceivedMsg(node, content, tcb);
W
wenjun 已提交
1188 1189 1190
            if (ret == LOS_OK) {
                break;
            }
1191
            if (ret == -ENOENT) { /* It means that we've received a failed reply */
W
wenjun 已提交
1192 1193 1194 1195
                return ret;
            }
        }
    } while (1);
Z
zhushengle 已提交
1196 1197 1198 1199
    node->msg.data = (VOID *)GetIpcUserAddr(OsCurrProcessGet(), (INTPTR)(node->msg.data));
    node->msg.offsets = (VOID *)GetIpcUserAddr(OsCurrProcessGet(), (INTPTR)(node->msg.offsets));
    content->inMsg = (VOID *)GetIpcUserAddr(OsCurrProcessGet(), (INTPTR)(&(node->msg)));
    EnableIpcNodeFreeByUser(OsCurrProcessGet(), (VOID *)node);
W
wenjun 已提交
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
    return LOS_OK;
}

LITE_OS_SEC_TEXT STATIC UINT32 LiteIpcMsgHandle(IpcContent *con)
{
    UINT32 ret = LOS_OK;
    IpcContent localContent;
    IpcContent *content = &localContent;
    IpcMsg localMsg;
    IpcMsg *msg = &localMsg;
    IpcListNode *nodeNeedFree = NULL;

    if (copy_from_user((void *)content, (const void *)con, sizeof(IpcContent)) != LOS_OK) {
        PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
        return -EINVAL;
    }

    if ((content->flag & BUFF_FREE) == BUFF_FREE) {
        ret = CheckUsedBuffer(content->buffToFree, &nodeNeedFree);
        if (ret != LOS_OK) {
            PRINT_ERR("CheckUsedBuffer failed:%d\n", ret);
            return ret;
        }
    }

    if ((content->flag & SEND) == SEND) {
        if (content->outMsg == NULL) {
            PRINT_ERR("content->outmsg is null\n");
            ret = -EINVAL;
            goto BUFFER_FREE;
        }
        if (copy_from_user((void *)msg, (const void *)content->outMsg, sizeof(IpcMsg)) != LOS_OK) {
            PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
            ret = -EINVAL;
            goto BUFFER_FREE;
        }
        content->outMsg = msg;
        if ((content->outMsg->type < 0) || (content->outMsg->type >= MT_DEATH_NOTIFY)) {
X
x_xiny 已提交
1238
            PRINT_ERR("LiteIpc unknown msg type:%d\n", content->outMsg->type);
W
wenjun 已提交
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
            ret = -EINVAL;
            goto BUFFER_FREE;
        }
        ret = LiteIpcWrite(content);
        if (ret != LOS_OK) {
            PRINT_ERR("LiteIpcWrite failed\n");
            goto BUFFER_FREE;
        }
    }
BUFFER_FREE:
    if (nodeNeedFree != NULL) {
Z
zhushengle 已提交
1250
        UINT32 freeRet = LiteIpcNodeFree(OsCurrProcessGet(), nodeNeedFree);
W
wenjun 已提交
1251 1252 1253 1254 1255 1256 1257 1258 1259
        ret = (freeRet == LOS_OK) ? ret : freeRet;
    }
    if (ret != LOS_OK) {
        return ret;
    }

    if ((content->flag & RECV) == RECV) {
        ret = LiteIpcRead(content);
        if (ret != LOS_OK) {
1260
            PRINT_ERR("LiteIpcRead failed ERROR: %d\n", (INT32)ret);
W
wenjun 已提交
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
            return ret;
        }
        UINT32 offset = LOS_OFF_SET_OF(IpcContent, inMsg);
        ret = copy_to_user((char*)con + offset, (char*)content + offset, sizeof(IpcMsg *));
        if (ret != LOS_OK) {
            PRINT_ERR("%s, %d, %d\n", __FUNCTION__, __LINE__, ret);
            return -EINVAL;
        }
    }
    return ret;
}

LITE_OS_SEC_TEXT STATIC UINT32 HandleCmsCmd(CmsCmdContent *content)
{
    UINT32 ret = LOS_OK;
    CmsCmdContent localContent;
    if (content == NULL) {
        return -EINVAL;
    }
    if (IsCmsTask(LOS_CurTaskIDGet()) == FALSE) {
        return -EACCES;
    }
    if (copy_from_user((void *)(&localContent), (const void *)content, sizeof(CmsCmdContent)) != LOS_OK) {
        PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
        return -EINVAL;
    }
    switch (localContent.cmd) {
        case CMS_GEN_HANDLE:
            if ((localContent.taskID != 0) && (IsTaskAlive(localContent.taskID) == FALSE)) {
                return -EINVAL;
            }
            ret = GenerateServiceHandle(localContent.taskID, HANDLE_REGISTED, &(localContent.serviceHandle));
            if (ret == LOS_OK) {
                ret = copy_to_user((void *)content, (const void *)(&localContent), sizeof(CmsCmdContent));
            }
C
Caoruihong 已提交
1296
            (VOID)LOS_MuxLock(&g_serviceHandleMapMux, LOS_WAIT_FOREVER);
W
wenjun 已提交
1297
            AddServiceAccess(g_cmsTask.taskID, localContent.serviceHandle);
C
Caoruihong 已提交
1298
            (VOID)LOS_MuxUnlock(&g_serviceHandleMapMux);
W
wenjun 已提交
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
            break;
        case CMS_REMOVE_HANDLE:
            if (localContent.serviceHandle >= MAX_SERVICE_NUM) {
                return -EINVAL;
            }
            RefreshServiceHandle(localContent.serviceHandle, -1);
            break;
        case CMS_ADD_ACCESS:
            if (IsTaskAlive(localContent.taskID) == FALSE) {
                return -EINVAL;
            }
            return AddServiceAccess(localContent.taskID, localContent.serviceHandle);
        default:
L
lihongjin 已提交
1312
            PRINT_DEBUG("Unknown cmd cmd:%d\n", localContent.cmd);
W
wenjun 已提交
1313 1314 1315 1316 1317
            return -EINVAL;
    }
    return ret;
}

1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
LITE_OS_SEC_TEXT STATIC UINT32 HandleGetVersion(IpcVersion *version)
{
    UINT32 ret = LOS_OK;
    IpcVersion localIpcVersion;
    if (version == NULL) {
        return -EINVAL;
    }

    localIpcVersion.driverVersion = DRIVER_VERSION;
    ret = copy_to_user((void *)version, (const void *)(&localIpcVersion), sizeof(IpcVersion));
    if (ret != LOS_OK) {
        PRINT_ERR("%s, %d\n", __FUNCTION__, __LINE__);
    }
    return ret; 
}

W
wangchenyang 已提交
1334
LITE_OS_SEC_TEXT int LiteIpcIoctl(struct file *filep, int cmd, unsigned long arg)
W
wenjun 已提交
1335 1336
{
    UINT32 ret = LOS_OK;
Z
zhushengle 已提交
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
    LosProcessCB *pcb = OsCurrProcessGet();
    ProcIpcInfo *ipcInfo = pcb->ipcInfo;

    if (ipcInfo == NULL) {
        PRINT_ERR("Liteipc pool not create!\n");
        return -EINVAL;
    }

    if (IsPoolMapped(ipcInfo) == FALSE) {
        PRINT_ERR("Liteipc Ipc pool not init, need to mmap first!\n");
W
wenjun 已提交
1347 1348
        return -ENOMEM;
    }
Z
zhushengle 已提交
1349

W
wenjun 已提交
1350 1351
    switch (cmd) {
        case IPC_SET_CMS:
Y
yinjiaming 已提交
1352
            return (INT32)SetCms(arg);
W
wenjun 已提交
1353
        case IPC_CMS_CMD:
Y
yinjiaming 已提交
1354
            return (INT32)HandleCmsCmd((CmsCmdContent *)(UINTPTR)arg);
1355 1356
        case IPC_GET_VERSION:
            return (INT32)HandleGetVersion((IpcVersion *)(UINTPTR)arg);
W
wenjun 已提交
1357 1358
        case IPC_SET_IPC_THREAD:
            if (IsCmsSet() == FALSE) {
Z
zhushengle 已提交
1359
                PRINT_ERR("Liteipc ServiceManager not set!\n");
W
wenjun 已提交
1360 1361
                return -EINVAL;
            }
Y
yinjiaming 已提交
1362
            return (INT32)SetIpcTask();
W
wenjun 已提交
1363 1364 1365 1366 1367
        case IPC_SEND_RECV_MSG:
            if (arg == 0) {
                return -EINVAL;
            }
            if (IsCmsSet() == FALSE) {
Z
zhushengle 已提交
1368
                PRINT_ERR("Liteipc ServiceManager not set!\n");
W
wenjun 已提交
1369 1370 1371 1372
                return -EINVAL;
            }
            ret = LiteIpcMsgHandle((IpcContent *)(UINTPTR)arg);
            if (ret != LOS_OK) {
Y
yinjiaming 已提交
1373
                return (INT32)ret;
W
wenjun 已提交
1374 1375 1376
            }
            break;
        default:
L
lihongjin 已提交
1377
            PRINT_ERR("Unknown liteipc ioctl cmd:%d\n", cmd);
W
wenjun 已提交
1378 1379
            return -EINVAL;
    }
Y
yinjiaming 已提交
1380
    return (INT32)ret;
W
wenjun 已提交
1381
}