-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUefiHost.c
399 lines (327 loc) · 7.28 KB
/
UefiHost.c
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
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
393
394
395
396
397
398
399
#include <lkl_host.h>
#include <iomem.h>
#include <jmp_buf.h>
#include <unistd.h>
#include <lk/kernel/semaphore.h>
#include <lk/kernel/mutex.h>
#include <lk/kernel/thread.h>
#include <lk/kernel/event.h>
#include "LKL.h"
static void print(const char *str, int len)
{
int ret __attribute__((unused));
DEBUG_CODE_BEGIN();
ret = write(STDOUT_FILENO, str, len);
DEBUG_CODE_END();
}
struct lkl_mutex {
int recursive;
mutex_t mutex;
semaphore_t sem;
};
struct lkl_sem {
semaphore_t sem;
};
static struct lkl_sem *sem_alloc(int count)
{
struct lkl_sem *sem;
sem = AllocatePool(sizeof(*sem));
if (!sem)
return NULL;
sem_init(&sem->sem, count);
return sem;
}
static void sem_free(struct lkl_sem *sem)
{
sem_destroy(&sem->sem);
FreePool(sem);
}
static void sem_up(struct lkl_sem *sem)
{
sem_post(&sem->sem, 1);
}
static void sem_down(struct lkl_sem *sem)
{
int err;
do {
thread_yield();
err = sem_wait(&sem->sem);
} while (err < 0);
}
static struct lkl_mutex *mutex_alloc(int recursive)
{
struct lkl_mutex *mutex = AllocatePool(sizeof(struct lkl_mutex));
if (!mutex)
return NULL;
if (recursive)
mutex_init(&mutex->mutex);
else
sem_init(&mutex->sem, 1);
mutex->recursive = recursive;
return mutex;
}
static void mutex_lock(struct lkl_mutex *mutex)
{
int err;
if (mutex->recursive)
mutex_acquire(&mutex->mutex);
else {
do {
thread_yield();
err = sem_wait(&mutex->sem);
} while (err < 0);
}
}
static void mutex_unlock(struct lkl_mutex *mutex)
{
if (mutex->recursive)
mutex_release(&mutex->mutex);
else {
sem_post(&mutex->sem, 1);
}
}
static void mutex_free(struct lkl_mutex *mutex)
{
if (mutex->recursive)
mutex_destroy(&mutex->mutex);
else
sem_destroy(&mutex->sem);
FreePool(mutex);
}
#define MS2100N(x) ((x)*(1000000/100))
STATIC EFI_EVENT mTimerEvent;
STATIC volatile lk_time_t ticks = 0;
VOID
EFIAPI
TimerCallback (
IN EFI_EVENT Event,
IN VOID *Context
)
{
ticks += 10;
if (thread_timer_tick()==INT_RESCHEDULE) {
thread_preempt();
}
}
lk_time_t current_time(void)
{
return ticks;
}
lk_bigtime_t current_time_hires(void)
{
lk_bigtime_t time;
time = (lk_bigtime_t)ticks * 1000;
return time;
}
void lkl_thread_init(void)
{
EFI_STATUS Status;
thread_init_early();
thread_init();
thread_create_idle();
thread_set_priority(DEFAULT_PRIORITY);
Status = gBS->CreateEvent (EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK, TimerCallback, NULL, &mTimerEvent);
ASSERT_EFI_ERROR (Status);
Status = gBS->SetTimer (mTimerEvent, TimerPeriodic, MS2100N(10));
ASSERT_EFI_ERROR (Status);
}
static lkl_thread_t lkl_thread_create(void (*fn)(void *), void *arg)
{
thread_t *thread = thread_create("lkl", (int (*)(void *))fn, arg, DEFAULT_PRIORITY, 2*1024*1024);
if (!thread)
return 0;
else {
thread_resume(thread);
return (lkl_thread_t) thread;
}
}
static void lkl_thread_detach(void)
{
thread_detach(get_current_thread());
}
static void lkl_thread_exit(void)
{
thread_exit(0);
}
static int lkl_thread_join(lkl_thread_t tid)
{
if (thread_join((thread_t *)tid, NULL, INFINITE_TIME))
return -1;
else
return 0;
}
static lkl_thread_t thread_self(void)
{
return (lkl_thread_t)get_current_thread();
}
static int thread_equal(lkl_thread_t a, lkl_thread_t b)
{
return a==b;
}
static unsigned long long time_ns(void)
{
return current_time()*1000000ULL;
}
typedef struct {
EFI_EVENT event;
void (*fn)(void *);
void *arg;
event_t cond;
thread_t *thread;
int request_stop;
} ltimer_t;
VOID
EFIAPI
LTimerCallback (
IN EFI_EVENT Event,
IN VOID *Context
)
{
ltimer_t *timer = Context;
event_signal(&timer->cond, 1);
}
static int timer_thread(void *pdata)
{
ltimer_t *timer = pdata;
while (!timer->request_stop) {
event_wait(&timer->cond);
if (timer->request_stop)
break;
timer->fn(timer->arg);
}
event_destroy(&timer->cond);
FreePool(timer);
return 0;
}
static void *timer_alloc(void (*fn)(void *), void *arg)
{
EFI_STATUS Status;
ltimer_t *timer = AllocatePool(sizeof(ltimer_t));
ASSERT(timer);
timer->fn = fn;
timer->arg = arg;
timer->request_stop = 0;
event_init(&timer->cond, 0, EVENT_FLAG_AUTOUNSIGNAL);
Status = gBS->CreateEvent (EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK, LTimerCallback, timer, &timer->event);
ASSERT_EFI_ERROR (Status);
timer->thread = thread_create("timer", timer_thread, timer, DEFAULT_PRIORITY, 1*1024*1024);
thread_detach_and_resume(timer->thread);
return timer;
}
static int timer_set_oneshot(void *_timer, unsigned long ns)
{
EFI_STATUS Status;
ltimer_t *timer = _timer;
Status = gBS->SetTimer (timer->event, TimerRelative, ns/100);
ASSERT_EFI_ERROR (Status);
return 0;
}
static void timer_free(void *_timer)
{
EFI_STATUS Status;
ltimer_t *timer = _timer;
Status = gBS->CloseEvent (timer->event);
ASSERT_EFI_ERROR (Status);
timer->request_stop = 1;
event_signal(&timer->cond, 1);
}
static void lkl_panic(void)
{
ASSERT(0);
}
static long _gettid(void)
{
return (long)get_current_thread();
}
static void *lkl_mem_alloc(unsigned long size)
{
return AllocatePool(size);
}
static void lkl_mem_free(void *ptr)
{
if (ptr) FreePool(ptr);
}
struct lkl_host_operations lkl_host_ops = {
.panic = lkl_panic,
.thread_create = lkl_thread_create,
.thread_detach = lkl_thread_detach,
.thread_exit = lkl_thread_exit,
.thread_join = lkl_thread_join,
.thread_self = thread_self,
.thread_equal = thread_equal,
.sem_alloc = sem_alloc,
.sem_free = sem_free,
.sem_up = sem_up,
.sem_down = sem_down,
.mutex_alloc = mutex_alloc,
.mutex_free = mutex_free,
.mutex_lock = mutex_lock,
.mutex_unlock = mutex_unlock,
.time = time_ns,
.timer_alloc = timer_alloc,
.timer_set_oneshot = timer_set_oneshot,
.timer_free = timer_free,
.print = print,
.mem_alloc = lkl_mem_alloc,
.mem_free = lkl_mem_free,
.ioremap = lkl_ioremap,
.iomem_access = lkl_iomem_access,
.virtio_devices = lkl_virtio_devs,
.gettid = _gettid,
.jmp_buf_set = jmp_buf_set,
.jmp_buf_longjmp = jmp_buf_longjmp,
};
static int uefi_blk_get_capacity(struct lkl_disk disk, unsigned long long *res)
{
LKL_VOLUME *Volume = disk.handle;
*res = (unsigned long long)((Volume->BlockIo->Media->LastBlock+1) * Volume->BlockIo->Media->BlockSize);
return 0;
}
static int do_rw(LKL_VOLUME *Volume, EFI_DISK_READ fn, struct lkl_disk disk, struct lkl_blk_req *req)
{
INT64 off = req->sector * 512;
void *addr;
int len;
int i;
int ret = 0;
EFI_STATUS Status;
for (i = 0; i < req->count; i++) {
addr = req->buf[i].iov_base;
len = req->buf[i].iov_len;
Status = fn(Volume->DiskIo, Volume->MediaId, off, len, addr);
if (EFI_ERROR(Status)) {
ret = -1;
goto out;
}
addr += len;
off += len;
}
out:
return ret;
}
static int uefi_blk_request(struct lkl_disk disk, struct lkl_blk_req *req)
{
LKL_VOLUME *Volume = disk.handle;
int err = 0;
switch (req->type) {
case LKL_DEV_BLK_TYPE_READ:
err = do_rw(Volume, Volume->DiskIo->ReadDisk, disk, req);
break;
case LKL_DEV_BLK_TYPE_WRITE:
err = do_rw(Volume, Volume->DiskIo->WriteDisk, disk, req);
break;
case LKL_DEV_BLK_TYPE_FLUSH:
case LKL_DEV_BLK_TYPE_FLUSH_OUT:
Volume->BlockIo->FlushBlocks(Volume->BlockIo);
break;
default:
return LKL_DEV_BLK_STATUS_UNSUP;
}
if (err < 0)
return LKL_DEV_BLK_STATUS_IOERR;
return LKL_DEV_BLK_STATUS_OK;
}
struct lkl_dev_blk_ops lkl_dev_blk_ops = {
.get_capacity = uefi_blk_get_capacity,
.request = uefi_blk_request,
};