los_hilog.c 10.1 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2 3
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (c) 2020-2021 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
 *
 * 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_hilog.h"
33
#include "los_init.h"
W
wenjun 已提交
34 35 36 37
#include "los_mp.h"
#include "los_mux.h"
#include "los_process_pri.h"
#include "los_task_pri.h"
M
mucor 已提交
38 39
#include "fs/file.h"
#include "fs/driver.h"
W
wenjun 已提交
40 41 42 43
#include "los_vm_map.h"
#include "los_vm_lock.h"
#include "user_copy.h"

44
#define HILOG_BUFFER LOSCFG_HILOG_BUFFER_SIZE
W
wenjun 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#define DRIVER_MODE 0666
#define HILOG_DRIVER "/dev/hilog"

struct HiLogEntry {
    unsigned int len;
    unsigned int hdrSize;
    unsigned int pid : 16;
    unsigned int taskId : 16;
    unsigned int sec;
    unsigned int nsec;
    unsigned int reserved;
    char msg[0];
};

ssize_t HilogRead(struct file *filep, char __user *buf, size_t count);
ssize_t HilogWrite(struct file *filep, const char __user *buf, size_t count);
W
wangchenyang 已提交
61 62
int HiLogOpen(struct file *filep);
int HiLogClose(struct file *filep);
W
wenjun 已提交
63

W
wangchenyang 已提交
64 65
static ssize_t HiLogWrite(struct file *filep, const char *buffer, size_t bufLen);
static ssize_t HiLogRead(struct file *filep, char *buffer, size_t bufLen);
W
wenjun 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

STATIC struct file_operations_vfs g_hilogFops = {
    HiLogOpen,  /* open */
    HiLogClose, /* close */
    HiLogRead,  /* read */
    HiLogWrite, /* write */
    NULL,       /* seek */
    NULL,       /* ioctl */
    NULL,       /* mmap */
#ifndef CONFIG_DISABLE_POLL
    NULL, /* poll */
#endif
    NULL, /* unlink */
};

W
wangchenyang 已提交
81
struct HiLogCharDevice {
W
wenjun 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    int flag;
    LosMux mtx;
    unsigned char *buffer;
    wait_queue_head_t wq;
    size_t writeOffset;
    size_t headOffset;
    size_t size;
    size_t count;
} g_hiLogDev;

static inline unsigned char *HiLogBufferHead(void)
{
    return g_hiLogDev.buffer + g_hiLogDev.headOffset;
}

W
wangchenyang 已提交
97
int HiLogOpen(struct file *filep)
W
wenjun 已提交
98 99 100 101 102
{
    (void)filep;
    return 0;
}

W
wangchenyang 已提交
103
int HiLogClose(struct file *filep)
W
wenjun 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
{
    (void)filep;
    return 0;
}

static void HiLogBufferInc(size_t sz)
{
    if (g_hiLogDev.size + sz <= HILOG_BUFFER) {
        g_hiLogDev.size += sz;
        g_hiLogDev.writeOffset += sz;
        g_hiLogDev.writeOffset %= HILOG_BUFFER;
        g_hiLogDev.count++;
    }
}

static void HiLogBufferDec(size_t sz)
{
    if (g_hiLogDev.size >= sz) {
        g_hiLogDev.size -= sz;
        g_hiLogDev.headOffset += sz;
        g_hiLogDev.headOffset %= HILOG_BUFFER;
        g_hiLogDev.count--;
    }
}

A
arvinzzz 已提交
129
static int HiLogBufferCopy(unsigned char *dst, unsigned dstLen, const unsigned char *src, size_t srcLen)
W
wenjun 已提交
130 131 132 133
{
    int retval = -1;
    size_t minLen = (dstLen > srcLen) ? srcLen : dstLen;

M
mamingshuai 已提交
134 135
    if (LOS_IsUserAddressRange((VADDR_T)(UINTPTR)dst, minLen) &&
        LOS_IsUserAddressRange((VADDR_T)(UINTPTR)src, minLen)) {
W
wenjun 已提交
136 137 138
        return retval;
    }

M
mamingshuai 已提交
139
    if (LOS_IsUserAddressRange((VADDR_T)(UINTPTR)dst, minLen)) {
W
wenjun 已提交
140
        retval = LOS_ArchCopyToUser(dst, src, minLen);
M
mamingshuai 已提交
141
    } else if (LOS_IsUserAddressRange((VADDR_T)(UINTPTR)src, minLen)) {
W
wenjun 已提交
142 143 144 145 146 147 148 149 150
        retval = LOS_ArchCopyFromUser(dst, src, minLen);
    } else {
        retval = memcpy_s(dst, dstLen, src, srcLen);
    }
    return retval;
}

static int HiLogReadRingBuffer(unsigned char *buffer, size_t bufLen)
{
W
wangchen 已提交
151
    int retval;
W
wenjun 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165
    size_t bufLeft = HILOG_BUFFER - g_hiLogDev.headOffset;
    if (bufLeft > bufLen) {
        retval = HiLogBufferCopy(buffer, bufLen, HiLogBufferHead(), bufLen);
    } else {
        retval = HiLogBufferCopy(buffer, bufLen, HiLogBufferHead(), bufLeft);
        if (retval < 0) {
            return retval;
        }

        retval = HiLogBufferCopy(buffer + bufLeft, bufLen - bufLeft, g_hiLogDev.buffer, bufLen - bufLeft);
    }
    return retval;
}

W
wangchenyang 已提交
166
static ssize_t HiLogRead(struct file *filep, char *buffer, size_t bufLen)
W
wenjun 已提交
167
{
W
wangchen 已提交
168
    int retval;
W
wenjun 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181
    struct HiLogEntry header;

    (void)filep;
    wait_event_interruptible(g_hiLogDev.wq, (g_hiLogDev.size > 0));

    (VOID)LOS_MuxAcquire(&g_hiLogDev.mtx);
    retval = HiLogReadRingBuffer((unsigned char *)&header, sizeof(header));
    if (retval < 0) {
        retval = -EINVAL;
        goto out;
    }

    if (bufLen < header.len + sizeof(header)) {
182
        PRINTK("buffer too small,bufLen=%d, header.len=%d,%d\n", bufLen, header.len, header.hdrSize);
W
wenjun 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
        retval = -ENOMEM;
        goto out;
    }

    HiLogBufferDec(sizeof(header));

    retval = HiLogBufferCopy((unsigned char *)buffer, bufLen, (unsigned char *)&header, sizeof(header));
    if (retval < 0) {
        retval = -EINVAL;
        goto out;
    }

    retval = HiLogReadRingBuffer((unsigned char *)(buffer + sizeof(header)), header.len);
    if (retval < 0) {
        retval = -EINVAL;
        goto out;
    }

    HiLogBufferDec(header.len);
    retval = header.len + sizeof(header);
out:
204 205 206 207 208 209 210
    if (retval == -ENOMEM) {
        // clean ring buffer
        g_hiLogDev.writeOffset = 0;
        g_hiLogDev.headOffset = 0;
        g_hiLogDev.size = 0;
        g_hiLogDev.count = 0;
    }
W
wenjun 已提交
211
    (VOID)LOS_MuxRelease(&g_hiLogDev.mtx);
W
wangchen 已提交
212
    return (ssize_t)retval;
W
wenjun 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
}

static int HiLogWriteRingBuffer(unsigned char *buffer, size_t bufLen)
{
    int retval;
    size_t bufLeft = HILOG_BUFFER - g_hiLogDev.writeOffset;
    if (bufLen > bufLeft) {
        retval = HiLogBufferCopy(g_hiLogDev.buffer + g_hiLogDev.writeOffset, bufLeft, buffer, bufLeft);
        if (retval) {
            return -1;
        }
        retval = HiLogBufferCopy(g_hiLogDev.buffer, HILOG_BUFFER, buffer + bufLeft, bufLen - bufLeft);
    } else {
        retval = HiLogBufferCopy(g_hiLogDev.buffer + g_hiLogDev.writeOffset, bufLeft, buffer, bufLen);
    }
    if (retval < 0) {
        return -1;
    }
    return 0;
}

static void HiLogHeadInit(struct HiLogEntry *header, size_t len)
{
Z
zhushengle 已提交
236 237
    struct timespec now = {0};
    (void)clock_gettime(CLOCK_REALTIME, &now);
W
wenjun 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251

    header->len = len;
    header->pid = LOS_GetCurrProcessID();
    header->taskId = LOS_CurTaskIDGet();
    header->sec = now.tv_sec;
    header->nsec = now.tv_nsec;
    header->hdrSize = sizeof(struct HiLogEntry);
}

static void HiLogCoverOldLog(size_t bufLen)
{
    int retval;
    struct HiLogEntry header;
    size_t totalSize = bufLen + sizeof(struct HiLogEntry);
252 253 254
    static int dropLogLines = 0;
    static int isLastTimeFull = 0;
    int isThisTimeFull = 0;
W
wenjun 已提交
255

L
laokz 已提交
256
    while (totalSize + g_hiLogDev.size > HILOG_BUFFER) {
W
wenjun 已提交
257 258 259 260 261
        retval = HiLogReadRingBuffer((unsigned char *)&header, sizeof(header));
        if (retval < 0) {
            break;
        }

262
        dropLogLines++;
263 264
        isThisTimeFull = 1;
        isLastTimeFull = 1;
L
laokz 已提交
265 266
        HiLogBufferDec(sizeof(header));
        HiLogBufferDec(header.len);
W
wenjun 已提交
267
    }
268 269 270
    if (isLastTimeFull == 1 && isThisTimeFull == 0) {
        /* so we can only print one log if hilog ring buffer is full in a short time */
        if (dropLogLines > 0) {
271
            PRINTK("hilog ringbuffer full, drop %d line(s) log\n", dropLogLines);
272 273 274
        }
        isLastTimeFull = 0;
        dropLogLines = 0;
275
    }
W
wenjun 已提交
276 277 278 279 280 281
}

int HiLogWriteInternal(const char *buffer, size_t bufLen)
{
    struct HiLogEntry header;
    int retval;
M
mamingshuai 已提交
282 283 284 285 286 287
    LosTaskCB *runTask =  (LosTaskCB *)OsCurrTaskGet();

    if ((g_hiLogDev.buffer == NULL) || (OS_INT_ACTIVE) || (runTask->taskStatus & OS_TASK_FLAG_SYSTEM_TASK)) {
        PRINTK("%s\n", buffer);
        return -EAGAIN;
    }
W
wenjun 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315

    (VOID)LOS_MuxAcquire(&g_hiLogDev.mtx);
    HiLogCoverOldLog(bufLen);
    HiLogHeadInit(&header, bufLen);

    retval = HiLogWriteRingBuffer((unsigned char *)&header, sizeof(header));
    if (retval) {
        retval = -ENODATA;
        goto out;
    }
    HiLogBufferInc(sizeof(header));

    retval = HiLogWriteRingBuffer((unsigned char *)(buffer), header.len);
    if (retval) {
        retval = -ENODATA;
        goto out;
    }

    HiLogBufferInc(header.len);

    retval = header.len;

out:
    (VOID)LOS_MuxRelease(&g_hiLogDev.mtx);
    if (retval > 0) {
        wake_up_interruptible(&g_hiLogDev.wq);
    }
    if (retval < 0) {
316
        PRINTK("write fail retval=%d\n", retval);
W
wenjun 已提交
317 318 319 320
    }
    return retval;
}

W
wangchenyang 已提交
321
static ssize_t HiLogWrite(struct file *filep, const char *buffer, size_t bufLen)
W
wenjun 已提交
322 323 324
{
    (void)filep;
    if (bufLen + sizeof(struct HiLogEntry) > HILOG_BUFFER) {
325
        PRINTK("input too large\n");
W
wenjun 已提交
326 327 328 329 330 331 332 333 334 335
        return -ENOMEM;
    }

    return HiLogWriteInternal(buffer, bufLen);
}

static void HiLogDeviceInit(void)
{
    g_hiLogDev.buffer = LOS_MemAlloc((VOID *)OS_SYS_MEM_ADDR, HILOG_BUFFER);
    if (g_hiLogDev.buffer == NULL) {
336
        PRINTK("In %s line %d,LOS_MemAlloc fail\n", __FUNCTION__, __LINE__);
W
wenjun 已提交
337 338 339 340 341 342 343 344 345 346 347
    }

    init_waitqueue_head(&g_hiLogDev.wq);
    LOS_MuxInit(&g_hiLogDev.mtx, NULL);

    g_hiLogDev.writeOffset = 0;
    g_hiLogDev.headOffset = 0;
    g_hiLogDev.size = 0;
    g_hiLogDev.count = 0;
}

348
int OsHiLogDriverInit(VOID)
W
wenjun 已提交
349 350 351 352 353
{
    HiLogDeviceInit();
    return register_driver(HILOG_DRIVER, &g_hilogFops, DRIVER_MODE, NULL);
}

354
LOS_MODULE_INIT(OsHiLogDriverInit, LOS_INIT_LEVEL_KMOD_EXTENDED);