shm.c 24.1 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 34 35 36 37
 *
 * 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 "stdio.h"
#include "string.h"
#include "time.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "los_config.h"
38
#include "los_init.h"
W
wenjun 已提交
39 40 41 42 43 44 45 46 47
#include "los_vm_map.h"
#include "los_vm_filemap.h"
#include "los_vm_phys.h"
#include "los_arch_mmu.h"
#include "los_vm_page.h"
#include "los_vm_lock.h"
#include "los_process.h"
#include "los_process_pri.h"
#include "user_copy.h"
Z
zhushengle 已提交
48 49
#include "los_vm_shm_pri.h"
#include "sys/shm.h"
M
mamingshuai 已提交
50 51 52 53
#ifdef LOSCFG_SHELL
#include "shcmd.h"
#include "shell.h"
#endif
W
wenjun 已提交
54 55


M
mamingshuai 已提交
56 57
#ifdef LOSCFG_KERNEL_SHM

W
wenjun 已提交
58 59 60 61 62 63 64 65
#define SHM_SEG_FREE    0x2000
#define SHM_SEG_USED    0x4000
#define SHM_SEG_REMOVE  0x8000

#ifndef SHM_M
#define SHM_M   010000
#endif

M
mamingshuai 已提交
66 67 68 69
#ifndef SHM_X
#define SHM_X   0100
#endif

W
wenjun 已提交
70 71 72 73
#ifndef ACCESSPERMS
#define ACCESSPERMS (S_IRWXU | S_IRWXG | S_IRWXO)
#endif

M
mamingshuai 已提交
74 75 76 77
#define SHM_S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH)
#define SHM_S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH)
#define SHM_S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)

W
wenjun 已提交
78 79 80
#define SHM_GROUPE_TO_USER  3
#define SHM_OTHER_TO_USER   6

Z
zhushengle 已提交
81 82
#ifndef LOSCFG_IPC_CONTAINER
STATIC LosMux g_sysvShmMux;
W
wenjun 已提交
83 84

/* private data */
Z
zhushengle 已提交
85
STATIC struct shminfo g_shmInfo;
W
wenjun 已提交
86
STATIC struct shmIDSource *g_shmSegs = NULL;
M
mamingshuai 已提交
87
STATIC UINT32 g_shmUsedPageCount;
W
wenjun 已提交
88

Z
zhushengle 已提交
89 90 91 92 93 94 95 96 97 98 99
#define IPC_SHM_INFO            g_shmInfo
#define IPC_SHM_SYS_VSHM_MUTEX  g_sysvShmMux
#define IPC_SHM_SEGS            g_shmSegs
#define IPC_SHM_USED_PAGE_COUNT g_shmUsedPageCount
#endif

/* private macro */
#define SYSV_SHM_LOCK()     (VOID)LOS_MuxLock(&IPC_SHM_SYS_VSHM_MUTEX, LOS_WAIT_FOREVER)
#define SYSV_SHM_UNLOCK()   (VOID)LOS_MuxUnlock(&IPC_SHM_SYS_VSHM_MUTEX)

struct shmIDSource *OsShmCBInit(LosMux *sysvShmMux, struct shminfo *shmInfo, UINT32 *shmUsedPageCount)
W
wenjun 已提交
100 101 102 103
{
    UINT32 ret;
    UINT32 i;

Z
zhushengle 已提交
104 105 106 107 108
    if ((sysvShmMux == NULL) || (shmInfo == NULL) || (shmUsedPageCount == NULL)) {
        return NULL;
    }

    ret = LOS_MuxInit(sysvShmMux, NULL);
W
wenjun 已提交
109
    if (ret != LOS_OK) {
110
        goto ERROR;
W
wenjun 已提交
111 112
    }

Z
zhushengle 已提交
113 114 115 116 117 118 119 120 121
    shmInfo->shmmax = SHM_MAX;
    shmInfo->shmmin = SHM_MIN;
    shmInfo->shmmni = SHM_MNI;
    shmInfo->shmseg = SHM_SEG;
    shmInfo->shmall = SHM_ALL;

    struct shmIDSource *shmSegs = LOS_MemAlloc((VOID *)OS_SYS_MEM_ADDR, sizeof(struct shmIDSource) * shmInfo->shmmni);
    if (shmSegs == NULL) {
        (VOID)LOS_MuxDestroy(sysvShmMux);
122
        goto ERROR;
W
wenjun 已提交
123
    }
Z
zhushengle 已提交
124 125
    (VOID)memset_s(shmSegs, (sizeof(struct shmIDSource) * shmInfo->shmmni),
                   0, (sizeof(struct shmIDSource) * shmInfo->shmmni));
W
wenjun 已提交
126

Z
zhushengle 已提交
127 128 129 130
    for (i = 0; i < shmInfo->shmmni; i++) {
        shmSegs[i].status = SHM_SEG_FREE;
        shmSegs[i].ds.shm_perm.seq = i + 1;
        LOS_ListInit(&shmSegs[i].node);
W
wenjun 已提交
131
    }
Z
zhushengle 已提交
132
    *shmUsedPageCount = 0;
W
wenjun 已提交
133

Z
zhushengle 已提交
134
    return shmSegs;
135 136 137

ERROR:
    VM_ERR("ShmInit fail\n");
Z
zhushengle 已提交
138 139 140 141 142 143 144 145 146 147 148 149
    return NULL;
}

UINT32 ShmInit(VOID)
{
#ifndef LOSCFG_IPC_CONTAINER
    g_shmSegs = OsShmCBInit(&IPC_SHM_SYS_VSHM_MUTEX, &IPC_SHM_INFO, &IPC_SHM_USED_PAGE_COUNT);
    if (g_shmSegs == NULL) {
        return LOS_NOK;
    }
#endif
    return LOS_OK;
W
wenjun 已提交
150 151
}

152 153 154
LOS_MODULE_INIT(ShmInit, LOS_INIT_LEVEL_VM_COMPLETE);

UINT32 ShmDeinit(VOID)
W
wenjun 已提交
155 156 157
{
    UINT32 ret;

Z
zhushengle 已提交
158 159
    (VOID)LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, IPC_SHM_SEGS);
    IPC_SHM_SEGS = NULL;
W
wenjun 已提交
160

Z
zhushengle 已提交
161
    ret = LOS_MuxDestroy(&IPC_SHM_SYS_VSHM_MUTEX);
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
    if (ret != LOS_OK) {
        return -1;
    }

    return 0;
}

STATIC inline VOID ShmSetSharedFlag(struct shmIDSource *seg)
{
    LosVmPage *page = NULL;

    LOS_DL_LIST_FOR_EACH_ENTRY(page, &seg->node, LosVmPage, node) {
        OsSetPageShared(page);
    }
}

STATIC inline VOID ShmClearSharedFlag(struct shmIDSource *seg)
{
    LosVmPage *page = NULL;

    LOS_DL_LIST_FOR_EACH_ENTRY(page, &seg->node, LosVmPage, node) {
        OsCleanPageShared(page);
    }
}

STATIC VOID ShmPagesRefDec(struct shmIDSource *seg)
{
    LosVmPage *page = NULL;

    LOS_DL_LIST_FOR_EACH_ENTRY(page, &seg->node, LosVmPage, node) {
        LOS_AtomicDec(&page->refCounts);
    }
}

Z
zhushengle 已提交
196
STATIC INT32 ShmAllocSegCheck(key_t key, size_t *size, INT32 *segNum)
W
wenjun 已提交
197 198
{
    INT32 i;
Z
zhushengle 已提交
199 200
    if ((*size == 0) || (*size < IPC_SHM_INFO.shmmin) ||
        (*size > IPC_SHM_INFO.shmmax)) {
W
wenjun 已提交
201 202
        return -EINVAL;
    }
Z
zhushengle 已提交
203

Z
zhushengle 已提交
204 205
    *size = LOS_Align(*size, PAGE_SIZE);
    if ((IPC_SHM_USED_PAGE_COUNT + (*size >> PAGE_SHIFT)) > IPC_SHM_INFO.shmall) {
M
mamingshuai 已提交
206 207
        return -ENOMEM;
    }
W
wenjun 已提交
208

Z
zhushengle 已提交
209 210 211
    for (i = 0; i < IPC_SHM_INFO.shmmni; i++) {
        if (IPC_SHM_SEGS[i].status & SHM_SEG_FREE) {
            IPC_SHM_SEGS[i].status &= ~SHM_SEG_FREE;
Z
zhushengle 已提交
212
            *segNum = i;
W
wenjun 已提交
213 214 215 216
            break;
        }
    }

Z
zhushengle 已提交
217
    if (*segNum < 0) {
W
wenjun 已提交
218 219 220
        return -ENOSPC;
    }

Z
zhushengle 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
#ifdef LOSCFG_KERNEL_IPC_PLIMIT
    if (OsIPCLimitShmAlloc(*size) != LOS_OK) {
        return -ENOMEM;
    }
#endif
    return 0;
}

STATIC INT32 ShmAllocSeg(key_t key, size_t size, INT32 shmflg)
{
    INT32 segNum = -1;
    struct shmIDSource *seg = NULL;
    size_t count;

    INT32 ret = ShmAllocSegCheck(key, &size, &segNum);
    if (ret < 0) {
        return ret;
    }

Z
zhushengle 已提交
240
    seg = &IPC_SHM_SEGS[segNum];
W
wenjun 已提交
241 242 243 244
    count = LOS_PhysPagesAlloc(size >> PAGE_SHIFT, &seg->node);
    if (count != (size >> PAGE_SHIFT)) {
        (VOID)LOS_PhysPagesFree(&seg->node);
        seg->status = SHM_SEG_FREE;
Z
zhushengle 已提交
245 246 247
#ifdef LOSCFG_KERNEL_IPC_PLIMIT
        OsIPCLimitShmFree(size);
#endif
W
wenjun 已提交
248 249
        return -ENOMEM;
    }
Z
zhushengle 已提交
250

W
wenjun 已提交
251
    ShmSetSharedFlag(seg);
Z
zhushengle 已提交
252
    IPC_SHM_USED_PAGE_COUNT += size >> PAGE_SHIFT;
W
wenjun 已提交
253 254

    seg->status |= SHM_SEG_USED;
M
mamingshuai 已提交
255
    seg->ds.shm_perm.mode = (UINT32)shmflg & ACCESSPERMS;
W
wenjun 已提交
256 257 258 259 260 261 262 263 264 265 266 267
    seg->ds.shm_perm.key = key;
    seg->ds.shm_segsz = size;
    seg->ds.shm_perm.cuid = LOS_GetUserID();
    seg->ds.shm_perm.uid = LOS_GetUserID();
    seg->ds.shm_perm.cgid = LOS_GetGroupID();
    seg->ds.shm_perm.gid = LOS_GetGroupID();
    seg->ds.shm_lpid = 0;
    seg->ds.shm_nattch = 0;
    seg->ds.shm_cpid = LOS_GetCurrProcessID();
    seg->ds.shm_atime = 0;
    seg->ds.shm_dtime = 0;
    seg->ds.shm_ctime = time(NULL);
M
mamingshuai 已提交
268 269 270
#ifdef LOSCFG_SHELL
    (VOID)memcpy_s(seg->ownerName, OS_PCB_NAME_LEN, OsCurrProcessGet()->processName, OS_PCB_NAME_LEN);
#endif
W
wenjun 已提交
271 272 273 274

    return segNum;
}

Z
zhushengle 已提交
275
STATIC INLINE VOID ShmFreeSeg(struct shmIDSource *seg, UINT32 *shmUsedPageCount)
W
wenjun 已提交
276 277 278 279 280 281 282 283 284
{
    UINT32 count;

    ShmClearSharedFlag(seg);
    count = LOS_PhysPagesFree(&seg->node);
    if (count != (seg->ds.shm_segsz >> PAGE_SHIFT)) {
        VM_ERR("free physical pages failed, count = %d, size = %d", count, seg->ds.shm_segsz >> PAGE_SHIFT);
        return;
    }
Z
zhushengle 已提交
285 286 287
#ifdef LOSCFG_KERNEL_IPC_PLIMIT
    OsIPCLimitShmFree(seg->ds.shm_segsz);
#endif
Z
zhushengle 已提交
288 289 290
    if (shmUsedPageCount != NULL) {
        (*shmUsedPageCount) -= seg->ds.shm_segsz >> PAGE_SHIFT;
    }
W
wenjun 已提交
291 292 293 294 295 296 297 298 299
    seg->status = SHM_SEG_FREE;
    LOS_ListInit(&seg->node);
}

STATIC INT32 ShmFindSegByKey(key_t key)
{
    INT32 i;
    struct shmIDSource *seg = NULL;

Z
zhushengle 已提交
300 301
    for (i = 0; i < IPC_SHM_INFO.shmmni; i++) {
        seg = &IPC_SHM_SEGS[i];
W
wenjun 已提交
302 303 304 305 306 307 308 309 310
        if ((seg->status & SHM_SEG_USED) &&
            (seg->ds.shm_perm.key == key)) {
            return i;
        }
    }

    return -1;
}

M
mamingshuai 已提交
311
STATIC INT32 ShmSegValidCheck(INT32 segNum, size_t size, INT32 shmFlg)
W
wenjun 已提交
312
{
Z
zhushengle 已提交
313
    struct shmIDSource *seg = &IPC_SHM_SEGS[segNum];
W
wenjun 已提交
314 315 316 317 318

    if (size > seg->ds.shm_segsz) {
        return -EINVAL;
    }

M
mamingshuai 已提交
319
    if (((UINT32)shmFlg & (IPC_CREAT | IPC_EXCL)) ==
W
wenjun 已提交
320 321 322 323 324 325 326 327 328 329 330
        (IPC_CREAT | IPC_EXCL)) {
        return -EEXIST;
    }

    return segNum;
}

STATIC struct shmIDSource *ShmFindSeg(int shmid)
{
    struct shmIDSource *seg = NULL;

Z
zhushengle 已提交
331
    if ((shmid < 0) || (shmid >= IPC_SHM_INFO.shmmni)) {
W
wenjun 已提交
332 333 334 335
        set_errno(EINVAL);
        return NULL;
    }

Z
zhushengle 已提交
336
    seg = &IPC_SHM_SEGS[shmid];
H
Haryslee 已提交
337
    if ((seg->status & SHM_SEG_FREE) || ((seg->ds.shm_nattch == 0) && (seg->status & SHM_SEG_REMOVE))) {
W
wenjun 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
        set_errno(EIDRM);
        return NULL;
    }

    return seg;
}

STATIC VOID ShmVmmMapping(LosVmSpace *space, LOS_DL_LIST *pageList, VADDR_T vaddr, UINT32 regionFlags)
{
    LosVmPage *vmPage = NULL;
    VADDR_T va = vaddr;
    PADDR_T pa;
    STATUS_T ret;

    LOS_DL_LIST_FOR_EACH_ENTRY(vmPage, pageList, LosVmPage, node) {
        pa = VM_PAGE_TO_PHYS(vmPage);
        LOS_AtomicInc(&vmPage->refCounts);
        ret = LOS_ArchMmuMap(&space->archMmu, va, pa, 1, regionFlags);
        if (ret != 1) {
            VM_ERR("LOS_ArchMmuMap failed, ret = %d", ret);
        }
        va += PAGE_SIZE;
    }
}

VOID OsShmFork(LosVmSpace *space, LosVmMapRegion *oldRegion, LosVmMapRegion *newRegion)
{
    struct shmIDSource *seg = NULL;

    SYSV_SHM_LOCK();
    seg = ShmFindSeg(oldRegion->shmid);
    if (seg == NULL) {
        SYSV_SHM_UNLOCK();
        VM_ERR("shm fork failed!");
        return;
    }

    newRegion->shmid = oldRegion->shmid;
    newRegion->forkFlags = oldRegion->forkFlags;
    ShmVmmMapping(space, &seg->node, newRegion->range.base, newRegion->regionFlags);
    seg->ds.shm_nattch++;
    SYSV_SHM_UNLOCK();
}

VOID OsShmRegionFree(LosVmSpace *space, LosVmMapRegion *region)
{
    struct shmIDSource *seg = NULL;

    SYSV_SHM_LOCK();
    seg = ShmFindSeg(region->shmid);
    if (seg == NULL) {
        SYSV_SHM_UNLOCK();
        return;
    }

393
    LOS_ArchMmuUnmap(&space->archMmu, region->range.base, region->range.size >> PAGE_SHIFT);
W
wenjun 已提交
394 395
    ShmPagesRefDec(seg);
    seg->ds.shm_nattch--;
396
    if (seg->ds.shm_nattch <= 0 && (seg->status & SHM_SEG_REMOVE)) {
Z
zhushengle 已提交
397
        ShmFreeSeg(seg, &IPC_SHM_USED_PAGE_COUNT);
398 399
    } else {
        seg->ds.shm_dtime = time(NULL);
M
mamingshuai 已提交
400
        seg->ds.shm_lpid = LOS_GetCurrProcessID(); /* may not be the space's PID. */
W
wenjun 已提交
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
    }
    SYSV_SHM_UNLOCK();
}

BOOL OsIsShmRegion(LosVmMapRegion *region)
{
    return (region->regionFlags & VM_MAP_REGION_FLAG_SHM) ? TRUE : FALSE;
}

STATIC INT32 ShmSegUsedCount(VOID)
{
    INT32 i;
    INT32 count = 0;
    struct shmIDSource *seg = NULL;

Z
zhushengle 已提交
416 417
    for (i = 0; i < IPC_SHM_INFO.shmmni; i++) {
        seg = &IPC_SHM_SEGS[i];
W
wenjun 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
        if (seg->status & SHM_SEG_USED) {
            count++;
        }
    }
    return count;
}

STATIC INT32 ShmPermCheck(struct shmIDSource *seg, mode_t mode)
{
    INT32 uid = LOS_GetUserID();
    UINT32 tmpMode = 0;
    mode_t privMode = seg->ds.shm_perm.mode;
    mode_t accMode;

    if ((uid == seg->ds.shm_perm.uid) || (uid == seg->ds.shm_perm.cuid)) {
        tmpMode |= SHM_M;
        accMode = mode & S_IRWXU;
    } else if (LOS_CheckInGroups(seg->ds.shm_perm.gid) ||
               LOS_CheckInGroups(seg->ds.shm_perm.cgid)) {
        privMode <<= SHM_GROUPE_TO_USER;
        accMode = (mode & S_IRWXG) << SHM_GROUPE_TO_USER;
    } else {
        privMode <<= SHM_OTHER_TO_USER;
        accMode = (mode & S_IRWXO) << SHM_OTHER_TO_USER;
    }

    if (privMode & SHM_R) {
        tmpMode |= SHM_R;
    }

    if (privMode & SHM_W) {
        tmpMode |= SHM_W;
    }

M
mamingshuai 已提交
452 453 454 455
    if (privMode & SHM_X) {
        tmpMode |= SHM_X;
    }

W
wenjun 已提交
456 457
    if ((mode == SHM_M) && (tmpMode & SHM_M)) {
        return 0;
M
mamingshuai 已提交
458 459
    } else if (mode == SHM_M) {
        return EACCES;
W
wenjun 已提交
460 461
    }

M
mamingshuai 已提交
462
    if ((tmpMode & accMode) == accMode) {
W
wenjun 已提交
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
        return 0;
    } else {
        return EACCES;
    }
}

INT32 ShmGet(key_t key, size_t size, INT32 shmflg)
{
    INT32 ret;
    INT32 shmid;

    SYSV_SHM_LOCK();

    if (key == IPC_PRIVATE) {
        ret = ShmAllocSeg(key, size, shmflg);
    } else {
        ret = ShmFindSegByKey(key);
        if (ret < 0) {
M
mamingshuai 已提交
481
            if (((UINT32)shmflg & IPC_CREAT) == 0) {
W
wenjun 已提交
482 483 484 485 486 487 488
                ret = -ENOENT;
                goto ERROR;
            } else {
                ret = ShmAllocSeg(key, size, shmflg);
            }
        } else {
            shmid = ret;
489 490 491 492 493
            if (((UINT32)shmflg & IPC_CREAT) &&
                ((UINT32)shmflg & IPC_EXCL)) {
                ret = -EEXIST;
                goto ERROR;
            }
M
mamingshuai 已提交
494
            ret = ShmPermCheck(ShmFindSeg(shmid), (UINT32)shmflg & ACCESSPERMS);
W
wenjun 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
            if (ret != 0) {
                ret = -ret;
                goto ERROR;
            }
            ret = ShmSegValidCheck(shmid, size, shmflg);
        }
    }
    if (ret < 0) {
        goto ERROR;
    }

    SYSV_SHM_UNLOCK();

    return ret;
ERROR:
    set_errno(-ret);
    SYSV_SHM_UNLOCK();
    PRINT_DEBUG("%s %d, ret = %d\n", __FUNCTION__, __LINE__, ret);
    return -1;
}

M
mamingshuai 已提交
516
INT32 ShmatParamCheck(const VOID *shmaddr, INT32 shmflg)
W
wenjun 已提交
517
{
M
mamingshuai 已提交
518
    if (((UINT32)shmflg & SHM_REMAP) && (shmaddr == NULL)) {
W
wenjun 已提交
519 520 521 522
        return EINVAL;
    }

    if ((shmaddr != NULL) && !IS_PAGE_ALIGNED(shmaddr) &&
M
mamingshuai 已提交
523
        (((UINT32)shmflg & SHM_RND) == 0)) {
W
wenjun 已提交
524 525 526 527 528 529 530 531 532 533 534
        return EINVAL;
    }

    return 0;
}

LosVmMapRegion *ShmatVmmAlloc(struct shmIDSource *seg, const VOID *shmaddr,
                              INT32 shmflg, UINT32 prot)
{
    LosVmSpace *space = OsCurrProcessGet()->vmSpace;
    LosVmMapRegion *region = NULL;
M
mamingshuai 已提交
535 536
    UINT32 flags = MAP_ANONYMOUS | MAP_SHARED;
    UINT32 mapFlags = flags | MAP_FIXED;
W
wenjun 已提交
537 538 539 540
    VADDR_T vaddr;
    UINT32 regionFlags;
    INT32 ret;

M
mamingshuai 已提交
541 542 543 544
    if (shmaddr != NULL) {
        flags |= MAP_FIXED_NOREPLACE;
    }
    regionFlags = OsCvtProtFlagsToRegionFlags(prot, flags);
W
wenjun 已提交
545 546 547 548
    (VOID)LOS_MuxAcquire(&space->regionMux);
    if (shmaddr == NULL) {
        region = LOS_RegionAlloc(space, 0, seg->ds.shm_segsz, regionFlags, 0);
    } else {
M
mamingshuai 已提交
549
        if ((UINT32)shmflg & SHM_RND) {
W
wenjun 已提交
550 551 552 553
            vaddr = ROUNDDOWN((VADDR_T)(UINTPTR)shmaddr, SHMLBA);
        } else {
            vaddr = (VADDR_T)(UINTPTR)shmaddr;
        }
M
mamingshuai 已提交
554 555 556
        if (!((UINT32)shmflg & SHM_REMAP) && (LOS_RegionFind(space, vaddr) ||
            LOS_RegionFind(space, vaddr + seg->ds.shm_segsz - 1) ||
            LOS_RegionRangeFind(space, vaddr, seg->ds.shm_segsz - 1))) {
557 558
            ret = EINVAL;
            goto ERROR;
W
wenjun 已提交
559
        }
M
mamingshuai 已提交
560
        vaddr = (VADDR_T)LOS_MMap(vaddr, seg->ds.shm_segsz, prot, mapFlags, -1, 0);
561
        region = LOS_RegionFind(space, vaddr);
W
wenjun 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
    }

    if (region == NULL) {
        ret = ENOMEM;
        goto ERROR;
    }
    ShmVmmMapping(space, &seg->node, region->range.base, regionFlags);
    (VOID)LOS_MuxRelease(&space->regionMux);
    return region;
ERROR:
    set_errno(ret);
    (VOID)LOS_MuxRelease(&space->regionMux);
    return NULL;
}

VOID *ShmAt(INT32 shmid, const VOID *shmaddr, INT32 shmflg)
{
    INT32 ret;
    UINT32 prot = PROT_READ;
M
mamingshuai 已提交
581
    mode_t acc_mode = SHM_S_IRUGO;
W
wenjun 已提交
582 583 584 585 586 587 588 589 590 591 592
    struct shmIDSource *seg = NULL;
    LosVmMapRegion *r = NULL;

    ret = ShmatParamCheck(shmaddr, shmflg);
    if (ret != 0) {
        set_errno(ret);
        return (VOID *)-1;
    }

    if ((UINT32)shmflg & SHM_EXEC) {
        prot |= PROT_EXEC;
M
mamingshuai 已提交
593
        acc_mode |= SHM_S_IXUGO;
W
wenjun 已提交
594 595
    } else if (((UINT32)shmflg & SHM_RDONLY) == 0) {
        prot |= PROT_WRITE;
M
mamingshuai 已提交
596
        acc_mode |= SHM_S_IWUGO;
W
wenjun 已提交
597 598 599 600 601 602 603 604 605
    }

    SYSV_SHM_LOCK();
    seg = ShmFindSeg(shmid);
    if (seg == NULL) {
        SYSV_SHM_UNLOCK();
        return (VOID *)-1;
    }

M
mamingshuai 已提交
606
    ret = ShmPermCheck(seg, acc_mode);
W
wenjun 已提交
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
    if (ret != 0) {
        goto ERROR;
    }

    seg->ds.shm_nattch++;
    r = ShmatVmmAlloc(seg, shmaddr, shmflg, prot);
    if (r == NULL) {
        seg->ds.shm_nattch--;
        SYSV_SHM_UNLOCK();
        return (VOID *)-1;
    }

    r->shmid = shmid;
    r->regionFlags |= VM_MAP_REGION_FLAG_SHM;
    seg->ds.shm_atime = time(NULL);
    seg->ds.shm_lpid = LOS_GetCurrProcessID();
    SYSV_SHM_UNLOCK();

    return (VOID *)(UINTPTR)r->range.base;
ERROR:
    set_errno(ret);
    SYSV_SHM_UNLOCK();
    PRINT_DEBUG("%s %d, ret = %d\n", __FUNCTION__, __LINE__, ret);
    return (VOID *)-1;
}

INT32 ShmCtl(INT32 shmid, INT32 cmd, struct shmid_ds *buf)
{
    struct shmIDSource *seg = NULL;
    INT32 ret = 0;
637
    struct shm_info shmInfo = { 0 };
W
wenjun 已提交
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
    struct ipc_perm shm_perm;

    cmd = ((UINT32)cmd & ~IPC_64);

    SYSV_SHM_LOCK();

    if ((cmd != IPC_INFO) && (cmd != SHM_INFO)) {
        seg = ShmFindSeg(shmid);
        if (seg == NULL) {
            SYSV_SHM_UNLOCK();
            return -1;
        }
    }

    if ((buf == NULL) && (cmd != IPC_RMID)) {
        ret = EINVAL;
        goto ERROR;
    }

    switch (cmd) {
        case IPC_STAT:
        case SHM_STAT:
M
mamingshuai 已提交
660
            ret = ShmPermCheck(seg, SHM_S_IRUGO);
W
wenjun 已提交
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
            if (ret != 0) {
                goto ERROR;
            }

            ret = LOS_ArchCopyToUser(buf, &seg->ds, sizeof(struct shmid_ds));
            if (ret != 0) {
                ret = EFAULT;
                goto ERROR;
            }
            if (cmd == SHM_STAT) {
                ret = (unsigned int)((unsigned int)seg->ds.shm_perm.seq << 16) | (unsigned int)((unsigned int)shmid & 0xffff); /* 16: use the seq as the upper 16 bits */
            }
            break;
        case IPC_SET:
            ret = ShmPermCheck(seg, SHM_M);
            if (ret != 0) {
                ret = EPERM;
                goto ERROR;
            }

            ret = LOS_ArchCopyFromUser(&shm_perm, &buf->shm_perm, sizeof(struct ipc_perm));
            if (ret != 0) {
                ret = EFAULT;
                goto ERROR;
            }
            seg->ds.shm_perm.uid = shm_perm.uid;
            seg->ds.shm_perm.gid = shm_perm.gid;
            seg->ds.shm_perm.mode = (seg->ds.shm_perm.mode & ~ACCESSPERMS) |
                                    (shm_perm.mode & ACCESSPERMS);
            seg->ds.shm_ctime = time(NULL);
M
mamingshuai 已提交
691 692 693 694
#ifdef LOSCFG_SHELL
            (VOID)memcpy_s(seg->ownerName, OS_PCB_NAME_LEN, OS_PCB_FROM_PID(shm_perm.uid)->processName,
                           OS_PCB_NAME_LEN);
#endif
W
wenjun 已提交
695 696 697 698 699 700 701 702 703 704
            break;
        case IPC_RMID:
            ret = ShmPermCheck(seg, SHM_M);
            if (ret != 0) {
                ret = EPERM;
                goto ERROR;
            }

            seg->status |= SHM_SEG_REMOVE;
            if (seg->ds.shm_nattch <= 0) {
Z
zhushengle 已提交
705
                ShmFreeSeg(seg, &IPC_SHM_USED_PAGE_COUNT);
W
wenjun 已提交
706 707 708
            }
            break;
        case IPC_INFO:
Z
zhushengle 已提交
709
            ret = LOS_ArchCopyToUser(buf, &IPC_SHM_INFO, sizeof(struct shminfo));
W
wenjun 已提交
710 711 712 713
            if (ret != 0) {
                ret = EFAULT;
                goto ERROR;
            }
Z
zhushengle 已提交
714
            ret = IPC_SHM_INFO.shmmni;
W
wenjun 已提交
715 716 717 718 719 720 721 722 723 724 725 726 727
            break;
        case SHM_INFO:
            shmInfo.shm_rss = 0;
            shmInfo.shm_swp = 0;
            shmInfo.shm_tot = 0;
            shmInfo.swap_attempts = 0;
            shmInfo.swap_successes = 0;
            shmInfo.used_ids = ShmSegUsedCount();
            ret = LOS_ArchCopyToUser(buf, &shmInfo, sizeof(struct shm_info));
            if (ret != 0) {
                ret = EFAULT;
                goto ERROR;
            }
Z
zhushengle 已提交
728
            ret = IPC_SHM_INFO.shmmni;
W
wenjun 已提交
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
            break;
        default:
            VM_ERR("the cmd(%d) is not supported!", cmd);
            ret = EINVAL;
            goto ERROR;
    }

    SYSV_SHM_UNLOCK();
    return ret;

ERROR:
    set_errno(ret);
    SYSV_SHM_UNLOCK();
    PRINT_DEBUG("%s %d, ret = %d\n", __FUNCTION__, __LINE__, ret);
    return -1;
}

INT32 ShmDt(const VOID *shmaddr)
{
    LosVmSpace *space = OsCurrProcessGet()->vmSpace;
    struct shmIDSource *seg = NULL;
    LosVmMapRegion *region = NULL;
    INT32 shmid;
    INT32 ret;

    if (IS_PAGE_ALIGNED(shmaddr) == 0) {
        ret = EINVAL;
        goto ERROR;
    }

    (VOID)LOS_MuxAcquire(&space->regionMux);
    region = LOS_RegionFind(space, (VADDR_T)(UINTPTR)shmaddr);
    if (region == NULL) {
        ret = EINVAL;
        goto ERROR_WITH_LOCK;
    }
    shmid = region->shmid;

    if (region->range.base != (VADDR_T)(UINTPTR)shmaddr) {
        ret = EINVAL;
        goto ERROR_WITH_LOCK;
    }

    /* remove it from aspace */
    LOS_RbDelNode(&space->regionRbTree, &region->rbNode);
    LOS_ArchMmuUnmap(&space->archMmu, region->range.base, region->range.size >> PAGE_SHIFT);
775
    (VOID)LOS_MuxRelease(&space->regionMux);
W
wenjun 已提交
776 777 778 779 780 781 782 783
    /* free it */
    free(region);

    SYSV_SHM_LOCK();
    seg = ShmFindSeg(shmid);
    if (seg == NULL) {
        ret = EINVAL;
        SYSV_SHM_UNLOCK();
784
        goto ERROR;
W
wenjun 已提交
785 786 787 788 789 790
    }

    ShmPagesRefDec(seg);
    seg->ds.shm_nattch--;
    if ((seg->ds.shm_nattch <= 0) &&
        (seg->status & SHM_SEG_REMOVE)) {
Z
zhushengle 已提交
791
        ShmFreeSeg(seg, &IPC_SHM_USED_PAGE_COUNT);
792 793 794
    } else {
        seg->ds.shm_dtime = time(NULL);
        seg->ds.shm_lpid = LOS_GetCurrProcessID();
W
wenjun 已提交
795 796
    }
    SYSV_SHM_UNLOCK();
797

W
wenjun 已提交
798 799 800 801 802 803 804 805 806 807
    return 0;

ERROR_WITH_LOCK:
    (VOID)LOS_MuxRelease(&space->regionMux);
ERROR:
    set_errno(ret);
    PRINT_DEBUG("%s %d, ret = %d\n", __FUNCTION__, __LINE__, ret);
    return -1;
}

Z
zhushengle 已提交
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
VOID OsShmCBDestroy(struct shmIDSource *shmSegs, struct shminfo *shmInfo, LosMux *sysvShmMux)
{
    if ((shmSegs == NULL) || (shmInfo == NULL) || (sysvShmMux == NULL)) {
        return;
    }

    for (UINT32 index = 0; index < shmInfo->shmmni; index++) {
        struct shmIDSource *seg = &shmSegs[index];
        if (seg->status == SHM_SEG_FREE) {
            continue;
        }

        (VOID)LOS_MuxLock(sysvShmMux, LOS_WAIT_FOREVER);
        ShmFreeSeg(seg, NULL);
        (VOID)LOS_MuxUnlock(sysvShmMux);
    }

    (VOID)LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, shmSegs);
    (VOID)LOS_MuxDestroy(sysvShmMux);
}

M
mamingshuai 已提交
829 830 831 832 833 834 835 836 837
#ifdef LOSCFG_SHELL
STATIC VOID OsShmInfoCmd(VOID)
{
    INT32 i;
    struct shmIDSource *seg = NULL;

    PRINTK("\r\n------- Shared Memory Segments -------\n");
    PRINTK("key      shmid    perms      bytes      nattch     status     owner\n");
    SYSV_SHM_LOCK();
Z
zhushengle 已提交
838 839
    for (i = 0; i < IPC_SHM_INFO.shmmni; i++) {
        seg = &IPC_SHM_SEGS[i];
M
mamingshuai 已提交
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
        if (!(seg->status & SHM_SEG_USED)) {
            continue;
        }
        PRINTK("%08x %-8d %-10o %-10u %-10u %-10x %s\n", seg->ds.shm_perm.key,
               i, seg->ds.shm_perm.mode, seg->ds.shm_segsz, seg->ds.shm_nattch,
               seg->status, seg->ownerName);

    }
    SYSV_SHM_UNLOCK();
}

STATIC VOID OsShmDeleteCmd(INT32 shmid)
{
    struct shmIDSource *seg = NULL;

Z
zhushengle 已提交
855
    if ((shmid < 0) || (shmid >= IPC_SHM_INFO.shmmni)) {
M
mamingshuai 已提交
856 857 858 859 860 861 862 863 864 865 866 867
        PRINT_ERR("shmid is invalid: %d\n", shmid);
        return;
    }

    SYSV_SHM_LOCK();
    seg = ShmFindSeg(shmid);
    if (seg == NULL) {
        SYSV_SHM_UNLOCK();
        return;
    }

    if (seg->ds.shm_nattch <= 0) {
Z
zhushengle 已提交
868
        ShmFreeSeg(seg, &IPC_SHM_USED_PAGE_COUNT);
M
mamingshuai 已提交
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 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
    }
    SYSV_SHM_UNLOCK();
}

STATIC VOID OsShmCmdUsage(VOID)
{
    PRINTK("\tnone option,   print shm usage info\n"
           "\t-r [shmid],    Recycle the specified shared memory about shmid\n"
           "\t-h | --help,   print shm command usage\n");
}

UINT32 OsShellCmdShm(INT32 argc, const CHAR *argv[])
{
    INT32 shmid;
    CHAR *endPtr = NULL;

    if (argc == 0) {
        OsShmInfoCmd();
    } else if (argc == 1) {
        if ((strcmp(argv[0], "-h") != 0) && (strcmp(argv[0], "--help") != 0)) {
            PRINTK("Invalid option: %s\n", argv[0]);
        }
        OsShmCmdUsage();
    } else if (argc == 2) { /* 2: two parameter */
        if (strcmp(argv[0], "-r") != 0) {
            PRINTK("Invalid option: %s\n", argv[0]);
            goto DONE;
        }
        shmid = strtoul((CHAR *)argv[1], &endPtr, 0);
        if ((endPtr == NULL) || (*endPtr != 0)) {
            PRINTK("check shmid %s(us) invalid\n", argv[1]);
            goto DONE;
        }
        /* try to delete shm about shmid */
        OsShmDeleteCmd(shmid);
    }
    return 0;
DONE:
    OsShmCmdUsage();
    return -1;
}

SHELLCMD_ENTRY(shm_shellcmd, CMD_TYPE_SHOW, "shm", 2, (CmdCallBackFunc)OsShellCmdShm);
#endif
#endif