los_time_container.c 8.6 KB
Newer Older
Z
zhushengle 已提交
1 2 3 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
/*
 * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
 *
 * 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 "los_time_container_pri.h"
#include "los_process_pri.h"

#ifdef LOSCFG_TIME_CONTAINER
STATIC UINT32 g_currentTimeContainerNum;

STATIC TimeContainer *CreateNewTimeContainer(TimeContainer *parent)
{
    UINT32 size = sizeof(TimeContainer);
    TimeContainer *timeContainer = (TimeContainer *)LOS_MemAlloc(m_aucSysMem1, size);
    if (timeContainer == NULL) {
        return NULL;
    }
    (VOID)memset_s(timeContainer, size, 0, size);

    timeContainer->containerID = OsAllocContainerID();
    if (parent != NULL) {
        timeContainer->frozenOffsets = FALSE;
        LOS_AtomicSet(&timeContainer->rc, 1);
    } else {
        timeContainer->frozenOffsets = TRUE;
        LOS_AtomicSet(&timeContainer->rc, 3); /* 3: Three system processes */
    }
    return timeContainer;
}

STATIC UINT32 CreateTimeContainer(LosProcessCB *child, LosProcessCB *parent)
{
    UINT32 intSave;

    SCHEDULER_LOCK(intSave);
Z
zhushengle 已提交
61
    TimeContainer *newTimeContainer = parent->container->timeForChildContainer;
Z
zhushengle 已提交
62 63
    LOS_AtomicInc(&newTimeContainer->rc);
    newTimeContainer->frozenOffsets = TRUE;
Z
zhushengle 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    child->container->timeContainer = newTimeContainer;
    child->container->timeForChildContainer = newTimeContainer;
    SCHEDULER_UNLOCK(intSave);
    return LOS_OK;
}

UINT32 OsInitRootTimeContainer(TimeContainer **timeContainer)
{
    UINT32 intSave;
    TimeContainer *newTimeContainer = CreateNewTimeContainer(NULL);
    if (newTimeContainer == NULL) {
        return ENOMEM;
    }

    SCHEDULER_LOCK(intSave);
    *timeContainer = newTimeContainer;
    g_currentTimeContainerNum++;
    SCHEDULER_UNLOCK(intSave);
    return LOS_OK;
}

UINT32 OsCopyTimeContainer(UINTPTR flags, LosProcessCB *child, LosProcessCB *parent)
{
    UINT32 intSave;
    TimeContainer *currTimeContainer = parent->container->timeContainer;

Z
zhushengle 已提交
90
    if (currTimeContainer == parent->container->timeForChildContainer) {
Z
zhushengle 已提交
91 92 93 94 95 96 97 98
        SCHEDULER_LOCK(intSave);
        LOS_AtomicInc(&currTimeContainer->rc);
        child->container->timeContainer = currTimeContainer;
        child->container->timeForChildContainer = currTimeContainer;
        SCHEDULER_UNLOCK(intSave);
        return LOS_OK;
    }

Z
zhushengle 已提交
99 100 101 102
    if (OsContainerLimitCheck(TIME_CONTAINER, &g_currentTimeContainerNum) != LOS_OK) {
        return EPERM;
    }

Z
zhushengle 已提交
103 104 105
    return CreateTimeContainer(child, parent);
}

Z
zhushengle 已提交
106 107 108 109 110 111 112
UINT32 OsUnshareTimeContainer(UINTPTR flags, LosProcessCB *curr, Container *newContainer)
{
    UINT32 intSave;
    if (!(flags & CLONE_NEWTIME)) {
        SCHEDULER_LOCK(intSave);
        newContainer->timeContainer = curr->container->timeContainer;
        newContainer->timeForChildContainer = curr->container->timeForChildContainer;
Z
zhushengle 已提交
113
        LOS_AtomicInc(&newContainer->timeContainer->rc);
Z
zhushengle 已提交
114 115 116
        if (newContainer->timeContainer != newContainer->timeForChildContainer) {
            LOS_AtomicInc(&newContainer->timeForChildContainer->rc);
        }
Z
zhushengle 已提交
117 118 119 120
        SCHEDULER_UNLOCK(intSave);
        return LOS_OK;
    }

Z
zhushengle 已提交
121 122 123 124
    if (OsContainerLimitCheck(TIME_CONTAINER, &g_currentTimeContainerNum) != LOS_OK) {
        return EPERM;
    }

Z
zhushengle 已提交
125 126 127 128 129 130 131 132 133 134 135 136
    TimeContainer *timeForChild = CreateNewTimeContainer(curr->container->timeContainer);
    if (timeForChild == NULL) {
        return ENOMEM;
    }

    SCHEDULER_LOCK(intSave);
    if (curr->container->timeContainer != curr->container->timeForChildContainer) {
        SCHEDULER_UNLOCK(intSave);
        (VOID)LOS_MemFree(m_aucSysMem1, timeForChild);
        return EINVAL;
    }

Z
zhushengle 已提交
137 138
    (VOID)memcpy_s(&timeForChild->monotonic, sizeof(struct timespec64),
                   &curr->container->timeContainer->monotonic, sizeof(struct timespec64));
Z
zhushengle 已提交
139
    newContainer->timeContainer = curr->container->timeContainer;
Z
zhushengle 已提交
140
    LOS_AtomicInc(&newContainer->timeContainer->rc);
Z
zhushengle 已提交
141
    newContainer->timeForChildContainer = timeForChild;
Z
zhushengle 已提交
142
    g_currentTimeContainerNum++;
Z
zhushengle 已提交
143 144 145 146
    SCHEDULER_UNLOCK(intSave);
    return LOS_OK;
}

Z
zhushengle 已提交
147
UINT32 OsSetNsTimeContainer(UINT32 flags, Container *container, Container *newContainer)
Z
zhushengle 已提交
148
{
Z
zhushengle 已提交
149 150 151 152 153 154 155
    LosProcessCB *curr = OsCurrProcessGet();
    if (flags & CLONE_NEWTIME) {
        container->timeContainer->frozenOffsets = TRUE;
        newContainer->timeContainer = container->timeContainer;
        newContainer->timeForChildContainer = container->timeContainer;
        LOS_AtomicInc(&container->timeContainer->rc);
        return LOS_OK;
Z
zhushengle 已提交
156 157
    }

Z
zhushengle 已提交
158 159 160 161 162
    newContainer->timeContainer = curr->container->timeContainer;
    LOS_AtomicInc(&curr->container->timeContainer->rc);
    newContainer->timeForChildContainer = curr->container->timeForChildContainer;
    if (curr->container->timeContainer != curr->container->timeForChildContainer) {
        LOS_AtomicInc(&curr->container->timeForChildContainer->rc);
Z
zhushengle 已提交
163
    }
Z
zhushengle 已提交
164
    return LOS_OK;
Z
zhushengle 已提交
165 166
}

Z
zhushengle 已提交
167
VOID OsTimeContainerDestroy(Container *container)
Z
zhushengle 已提交
168 169
{
    UINT32 intSave;
Z
zhushengle 已提交
170 171 172
    TimeContainer *timeContainer = NULL;
    TimeContainer *timeForChild = NULL;

Z
zhushengle 已提交
173
    if (container == NULL) {
Z
zhushengle 已提交
174 175 176 177
        return;
    }

    SCHEDULER_LOCK(intSave);
Z
zhushengle 已提交
178
    if (container->timeContainer == NULL) {
Z
zhushengle 已提交
179 180 181 182
        SCHEDULER_UNLOCK(intSave);
        return;
    }

Z
zhushengle 已提交
183 184 185
    if ((container->timeForChildContainer != NULL) && (container->timeContainer != container->timeForChildContainer)) {
        LOS_AtomicDec(&container->timeForChildContainer->rc);
        if (LOS_AtomicRead(&container->timeForChildContainer->rc) <= 0) {
Z
zhushengle 已提交
186
            g_currentTimeContainerNum--;
Z
zhushengle 已提交
187 188
            timeForChild = container->timeForChildContainer;
            container->timeForChildContainer = NULL;
Z
zhushengle 已提交
189 190
        }
    }
Z
zhushengle 已提交
191

Z
zhushengle 已提交
192 193
    LOS_AtomicDec(&container->timeContainer->rc);
    if (LOS_AtomicRead(&container->timeContainer->rc) <= 0) {
Z
zhushengle 已提交
194
        g_currentTimeContainerNum--;
Z
zhushengle 已提交
195 196 197
        timeContainer = container->timeContainer;
        container->timeContainer = NULL;
        container->timeForChildContainer = NULL;
Z
zhushengle 已提交
198
    }
Z
zhushengle 已提交
199
    SCHEDULER_UNLOCK(intSave);
Z
zhushengle 已提交
200 201
    (VOID)LOS_MemFree(m_aucSysMem1, timeForChild);
    (VOID)LOS_MemFree(m_aucSysMem1, timeContainer);
Z
zhushengle 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    return;
}

UINT32 OsGetTimeContainerID(TimeContainer *timeContainer)
{
    if (timeContainer == NULL) {
        return OS_INVALID_VALUE;
    }

    return timeContainer->containerID;
}

TimeContainer *OsGetCurrTimeContainer(VOID)
{
    return OsCurrProcessGet()->container->timeContainer;
}

UINT32 OsGetTimeContainerMonotonic(LosProcessCB *processCB, struct timespec64 *offsets)
{
    if ((processCB == NULL) || (offsets == NULL)) {
        return EINVAL;
    }

    if (OsProcessIsInactive(processCB)) {
        return ESRCH;
    }

    TimeContainer *timeContainer = processCB->container->timeForChildContainer;
    (VOID)memcpy_s(offsets, sizeof(struct timespec64), &timeContainer->monotonic, sizeof(struct timespec64));
    return LOS_OK;
}

UINT32 OsSetTimeContainerMonotonic(LosProcessCB *processCB, struct timespec64 *offsets)
{
    if ((processCB == NULL) || (offsets == NULL)) {
        return EINVAL;
    }

    if (OsProcessIsInactive(processCB)) {
        return ESRCH;
    }

    TimeContainer *timeContainer = processCB->container->timeForChildContainer;
    if (timeContainer->frozenOffsets) {
        return EACCES;
    }

    timeContainer->monotonic.tv_sec = offsets->tv_sec;
    timeContainer->monotonic.tv_nsec = offsets->tv_nsec;
    return LOS_OK;
}
Z
zhushengle 已提交
253 254 255 256 257

UINT32 OsGetTimeContainerCount(VOID)
{
    return g_currentTimeContainerNum;
}
Z
zhushengle 已提交
258
#endif