los_time_container.c 8.3 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 99 100 101
        SCHEDULER_LOCK(intSave);
        LOS_AtomicInc(&currTimeContainer->rc);
        child->container->timeContainer = currTimeContainer;
        child->container->timeForChildContainer = currTimeContainer;
        SCHEDULER_UNLOCK(intSave);
        return LOS_OK;
    }

    return CreateTimeContainer(child, parent);
}

Z
zhushengle 已提交
102 103 104 105 106 107 108
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 已提交
109
        LOS_AtomicInc(&newContainer->timeContainer->rc);
Z
zhushengle 已提交
110 111 112
        if (newContainer->timeContainer != newContainer->timeForChildContainer) {
            LOS_AtomicInc(&newContainer->timeForChildContainer->rc);
        }
Z
zhushengle 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
        SCHEDULER_UNLOCK(intSave);
        return LOS_OK;
    }

    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 已提交
129 130
    (VOID)memcpy_s(&timeForChild->monotonic, sizeof(struct timespec64),
                   &curr->container->timeContainer->monotonic, sizeof(struct timespec64));
Z
zhushengle 已提交
131
    newContainer->timeContainer = curr->container->timeContainer;
Z
zhushengle 已提交
132
    LOS_AtomicInc(&newContainer->timeContainer->rc);
Z
zhushengle 已提交
133
    newContainer->timeForChildContainer = timeForChild;
Z
zhushengle 已提交
134
    g_currentTimeContainerNum++;
Z
zhushengle 已提交
135 136 137 138
    SCHEDULER_UNLOCK(intSave);
    return LOS_OK;
}

Z
zhushengle 已提交
139
UINT32 OsSetNsTimeContainer(UINT32 flags, Container *container, Container *newContainer)
Z
zhushengle 已提交
140
{
Z
zhushengle 已提交
141 142 143 144 145 146 147
    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 已提交
148 149
    }

Z
zhushengle 已提交
150 151 152 153 154
    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 已提交
155
    }
Z
zhushengle 已提交
156
    return LOS_OK;
Z
zhushengle 已提交
157 158
}

Z
zhushengle 已提交
159
VOID OsTimeContainerDestroy(Container *container)
Z
zhushengle 已提交
160 161
{
    UINT32 intSave;
Z
zhushengle 已提交
162 163 164
    TimeContainer *timeContainer = NULL;
    TimeContainer *timeForChild = NULL;

Z
zhushengle 已提交
165
    if (container == NULL) {
Z
zhushengle 已提交
166 167 168 169
        return;
    }

    SCHEDULER_LOCK(intSave);
Z
zhushengle 已提交
170
    if (container->timeContainer == NULL) {
Z
zhushengle 已提交
171 172 173 174
        SCHEDULER_UNLOCK(intSave);
        return;
    }

Z
zhushengle 已提交
175 176 177
    if ((container->timeForChildContainer != NULL) && (container->timeContainer != container->timeForChildContainer)) {
        LOS_AtomicDec(&container->timeForChildContainer->rc);
        if (LOS_AtomicRead(&container->timeForChildContainer->rc) <= 0) {
Z
zhushengle 已提交
178
            g_currentTimeContainerNum--;
Z
zhushengle 已提交
179 180
            timeForChild = container->timeForChildContainer;
            container->timeForChildContainer = NULL;
Z
zhushengle 已提交
181 182
        }
    }
Z
zhushengle 已提交
183

Z
zhushengle 已提交
184 185
    LOS_AtomicDec(&container->timeContainer->rc);
    if (LOS_AtomicRead(&container->timeContainer->rc) <= 0) {
Z
zhushengle 已提交
186
        g_currentTimeContainerNum--;
Z
zhushengle 已提交
187 188 189
        timeContainer = container->timeContainer;
        container->timeContainer = NULL;
        container->timeForChildContainer = NULL;
Z
zhushengle 已提交
190
    }
Z
zhushengle 已提交
191
    SCHEDULER_UNLOCK(intSave);
Z
zhushengle 已提交
192 193
    (VOID)LOS_MemFree(m_aucSysMem1, timeForChild);
    (VOID)LOS_MemFree(m_aucSysMem1, timeContainer);
Z
zhushengle 已提交
194 195 196 197 198 199 200 201 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
    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;
}
#endif