summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/platform/linux-sgx/sgx_time.c
blob: d090083ef0d473ed402c4f23368f2f1273a07f00 (plain)
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
61
62
63
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
129
130
131
132
133
134
135
/*
 * Copyright (C) 2019 Intel Corporation.  All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

#include "platform_api_vmcore.h"

#define TRACE_FUNC() os_printf("undefined %s\n", __FUNCTION__)
#define TRACE_OCALL_FAIL() os_printf("ocall %s failed!\n", __FUNCTION__)

int
ocall_clock_gettime(int *p_ret, unsigned clock_id, void *tp_buf,
                    unsigned int tp_buf_size);
int
ocall_clock_getres(int *p_ret, int clock_id, void *res_buf,
                   unsigned int res_buf_size);
int
ocall_utimensat(int *p_ret, int dirfd, const char *pathname,
                const void *times_buf, unsigned int times_buf_size, int flags);
int
ocall_futimens(int *p_ret, int fd, const void *times_buf,
               unsigned int times_buf_size);
int
ocall_clock_nanosleep(int *p_ret, unsigned clock_id, int flags,
                      const void *req_buf, unsigned int req_buf_size,
                      const void *rem_buf, unsigned int rem_buf_size);

uint64
os_time_get_boot_microsecond()
{
#ifndef SGX_DISABLE_WASI
    struct timespec ts;
    if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
        return 0;
    }

    return ((uint64)ts.tv_sec) * 1000 * 1000 + ((uint64)ts.tv_nsec) / 1000;
#else
    return 0;
#endif
}

#ifndef SGX_DISABLE_WASI

int
clock_getres(int clock_id, struct timespec *res)
{
    int ret;

    if (ocall_clock_getres(&ret, clock_id, (void *)res, sizeof(struct timespec))
        != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return -1;
    }

    if (ret == -1)
        errno = get_errno();

    return ret;
}

int
clock_gettime(clockid_t clock_id, struct timespec *tp)
{
    int ret;

    if (ocall_clock_gettime(&ret, clock_id, (void *)tp, sizeof(struct timespec))
        != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return -1;
    }

    if (ret == -1)
        errno = get_errno();

    return ret;
}

int
utimensat(int dirfd, const char *pathname, const struct timespec times[2],
          int flags)
{
    int ret;

    if (ocall_utimensat(&ret, dirfd, pathname, (void *)times,
                        sizeof(struct timespec) * 2, flags)
        != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return -1;
    }

    if (ret == -1)
        errno = get_errno();

    return ret;
}

int
futimens(int fd, const struct timespec times[2])
{
    int ret;

    if (ocall_futimens(&ret, fd, (void *)times, sizeof(struct timespec) * 2)
        != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return -1;
    }

    if (ret == -1)
        errno = get_errno();

    return ret;
}

int
clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *request,
                struct timespec *remain)
{
    int ret;

    if (ocall_clock_nanosleep(&ret, clock_id, flags, (void *)request,
                              sizeof(struct timespec), (void *)remain,
                              sizeof(struct timespec))
        != SGX_SUCCESS) {
        TRACE_OCALL_FAIL();
        return -1;
    }

    if (ret == -1)
        errno = get_errno();

    return ret;
}

#endif