tail_core 0.1.0

Core library for the Tail operating system
Documentation
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
// Copyright 2025, TAIL OS. All Rights Reserved.
//
// You must obtain a written license from and pay applicable license fees to TAIL OS
// before you may reproduce, modify, or distribute this software, or any work that
// includes all or part of this software.
//
// Free development licenses are available for evaluation, research, and non-commercial
// purposes, which may include access to the source code under these terms. Redistribution
// or commercial use without a license is strictly prohibited.
//
// This file may contain contributions from others. Please review this entire file for
// other proprietary rights or license notices, as well as the TAIL OS License Guide at
// https://tail-os.com/license-guide/ for more information.
//
// For licensing inquiries, visit https://tail-os.com or email license@tail-os.com.


#[inline(never)]    // If it's inlined and the registers are used outside the function, the registers might be overwritten
pub fn syscall_terminate_process(exit_code: i32) -> !
{
    unsafe {
        core::arch::asm!(
            "mov x0, {exit_code}",
            "mov x8, #0",
            "svc #0x51",
            exit_code = in(reg) exit_code,
            clobber_abi("C"),
            options(nostack, preserves_flags),
        );
    }
    panic!("This function should not return");
}

/// Publishes a message to a topic.
///
/// # Arguments
///
/// * `topic` - The name of the topic to publish to
/// * `message` - A reference to the message to publish
/// * `message_size` - The size of the message in bytes
///
/// # Safety
///
/// This function is unsafe because it performs a system call. The caller must ensure:
/// - `topic` is a valid UTF-8 string
/// - `message` points to valid data of at least `message_size` bytes
/// - The message data remains valid for the duration of the system call
///
/// # Register Layout
///
/// - x0: topic name pointer
/// - x1: topic name length
/// - x2: message pointer
/// - x3: message size
/// - x8: syscall number (#1: KERNEL_CALL_SEND_MESSAGE_TO_TOPIC)
#[inline(never)]    // If it's inlined and the registers are used outside the function, the registers might be overwritten
pub fn syscall_publish_to_topic<T: ?Sized>(topic: &str, message: &T, message_size: usize)
{
    unsafe {
        core::arch::asm!(
            "mov x3, {message_size}",
            "mov x2, {message_ptr}",
            "mov x1, {topic_len}",
            "mov x0, {topic_ptr}",
            "mov x8, #1",  // #1: KERNEL_CALL_SEND_MESSAGE_TO_TOPIC
            "svc #0x51",
            message_size = in(reg) message_size,
            message_ptr = in(reg) message as *const T as *const u8,
            topic_len = in(reg) topic.len(),
            topic_ptr = in(reg) topic.as_ptr(),
            clobber_abi("C"),
            options(nostack, preserves_flags),
        );
    }
}

/// Creates a new topic.
///
/// # Arguments
///
/// * `topic` - The name of the topic to create
///
/// # Safety
///
/// This function is unsafe because it performs a system call. The caller must ensure:
/// - `topic` is a valid UTF-8 string
/// - The topic name length does not exceed `TOPIC_NAME_MAX_LENGTH`
///
/// # Register Layout
///
/// - x0: topic name pointer
/// - x1: topic name length
/// - x8: syscall number (#2: KERNEL_CALL_CREATE_TOPIC)
///
/// # Deprecated
///
/// This function is deprecated. Use the zero-copy version `syscall_create_topic()` instead.
#[deprecated(note = "Use syscall_create_topic() for zero-copy topic creation. This function will be removed in a future version.")]
#[inline(never)]    // If it's inlined and the registers are used outside the function, the registers might be overwritten
pub fn syscall_create_topic_old<T: ?Sized>(topic: &str)
{
    unsafe {
        core::arch::asm!(
            "mov x1, {topic_len}",
            "mov x0, {topic_ptr}",
            "mov x8, #2",  // #2: KERNEL_CALL_CREATE_TOPIC
            "svc #0x51",
            topic_len = in(reg) topic.len(),
            topic_ptr = in(reg) topic.as_ptr(),
            clobber_abi("C"),
            options(nostack, preserves_flags),
        );
    }
}

#[inline(never)]
pub fn syscall_subscribe_topic(topic: &str) -> (*const u8, usize) {
    let mut message: *const u8;
    let mut message_size: usize;
    unsafe {
        core::arch::asm!(
            "mov x1, {topic_len}",
            "mov x0, {topic_ptr}",
            "mov x8, #3",  // #3: KERNEL_CALL_RECEIVE_MESSAGE_FROM_TOPIC
            "svc #0x51",
            topic_len = in(reg) topic.len(),
            topic_ptr = in(reg) topic.as_ptr(),
            out("x0") message,
            out("x1") message_size,
            clobber_abi("C"),
            options(nostack, preserves_flags),
        );
    }

    (message, message_size)
}

#[inline(never)]
pub fn syscall_mmap(virtual_address: u64, physical_address: u64, size: usize, prot: u64) -> *mut u8
{
    let mut out_virtual_address: *mut u8;

    unsafe {
        core::arch::asm!(
            "mov x3, {prot}",
            "mov x2, {size}",
            "mov x1, {physical_address}",
            "mov x0, {virtual_address}",
            "mov x8, #4",  // #4: KERNEL_CALL_MMAP
            "svc #0x51",
            prot = in(reg) prot,
            size = in(reg) size,
            physical_address = in(reg) physical_address,
            virtual_address = in(reg) virtual_address,
            out("x0") out_virtual_address,
            clobber_abi("C"),
            options(nostack, preserves_flags),
        );
    }

    out_virtual_address
}

#[inline(never)]
pub fn syscall_munmap(virtual_address: u64, size: usize) -> i32
{
    let mut result: i32 = 0;
    unsafe {
        core::arch::asm!(
            "mov x1, {size}",
            "mov x0, {virtual_address}",
            "mov x8, #5",  // #5: KERNEL_CALL_MUNMAP
            "svc #0x51",
            size = in(reg) size,
            virtual_address = in(reg) virtual_address,
            out("x0") result,
            clobber_abi("C"),
            options(nostack, preserves_flags),
        );
    }

    result
}

#[inline(never)]
pub fn syscall_request_service(server_name: &str, request_message_type: u64, request_message_ptr: *const u8, request_message_size: usize,
reply_message_ptr: *const u8, reply_message_size: usize) -> Result<i64, crate::error_kind::ErrorKind>
{
    let mut result: i64 = 0;

    unsafe {
        core::arch::asm!(
            "mov x6, {reply_size}",
            "mov x5, {reply_ptr}",
            "mov x4, {request_size}",
            "mov x3, {request_ptr}",
            "mov x2, {msg_type}",
            "mov x1, {name_len}",
            "mov x0, {name_ptr}",
            "mov x8, #6",  // #6: KERNEL_CALL_REQUEST_SERVICE
            "svc #0x51",
            reply_size = in(reg) reply_message_size,
            reply_ptr = in(reg) reply_message_ptr,
            request_size = in(reg) request_message_size,
            request_ptr = in(reg) request_message_ptr,
            msg_type = in(reg) request_message_type,
            name_len = in(reg) server_name.len(),
            name_ptr = in(reg) server_name.as_ptr(),
            out("x0") result,
            clobber_abi("C"),
            options(nostack, preserves_flags),
        );
    }

    if result < 0 {
        Err(crate::error_kind::ErrorKind::Other.into())
    } else {
        Ok(result)
    }
}

#[inline(never)]
pub fn syscall_send_service_request_and_wait_for_reply(server_name: &str, request_message_type: u64, request_message_ptr: *const u8,
request_message_size: usize) -> Result<(*const u8, usize, i64), crate::error_kind::ErrorKind>
{
    let mut reply_message_ptr: *const u8 = core::ptr::null();
    let mut reply_message_size: usize = 0;
    let mut reply_message_error: i64 = 0;

    unsafe {
        core::arch::asm!(
            "mov x4, {size}",
            "mov x3, {ptr}",
            "mov x2, {msg_type}",
            "mov x1, {name_len}",
            "mov x0, {name_ptr}",
            "mov x8, #10",  // #10: KERNEL_CALL_SEND_SERVICE_REQUEST_AND_WAIT_FOR_REPLY
            "svc #0x51",
            size = in(reg) request_message_size,
            ptr = in(reg) request_message_ptr,
            msg_type = in(reg) request_message_type,
            name_len = in(reg) server_name.len(),
            name_ptr = in(reg) server_name.as_ptr(),
            out("x0") reply_message_ptr,
            out("x1") reply_message_size,
            out("x2") reply_message_error,
            clobber_abi("C"),
            options(nostack, preserves_flags),
        );
    }

    if reply_message_error != 0 {   // 0 means None (i.e. no error)
        Err(crate::error_kind::ErrorKind::from(reply_message_error))
    } else {
        Ok((reply_message_ptr, reply_message_size, 0))
    }
}

#[inline(never)]
pub fn syscall_receive_service_request(server_name: &str) -> Result<(u64, u64, *const u8, usize), crate::error_kind::ErrorKind>
{
    let mut client_thread: *const u8;
    let mut request_message_type: u64;
    let mut request_message_ptr: *const u8;
    let mut request_message_size: usize;

    unsafe {
        core::arch::asm!(
            "mov x1, {name_len}",
            "mov x0, {name_ptr}",
            "mov x8, #11",  // #11: KERNEL_CALL_RECEIVE_SERVICE_REQUEST
            "svc #0x51",
            name_len = in(reg) server_name.len(),
            name_ptr = in(reg) server_name.as_ptr(),
            out("x0") client_thread,
            out("x1") request_message_type,
            out("x2") request_message_ptr,
            out("x3") request_message_size,
            clobber_abi("C"),
            options(nostack, preserves_flags),
        );
    }

    if client_thread == core::ptr::null() {
        Err(crate::error_kind::ErrorKind::Other.into())
    } else {
        Ok((client_thread as u64, request_message_type, request_message_ptr, request_message_size))
    }
}

#[inline(never)]
pub fn syscall_receive_request(server_name: &str) -> Result<(u64, u64, *const u8, usize, *const u8, usize), crate::error_kind::ErrorKind>
{
    let client_thread: *const u8;
    let request_message_type: u64;
    let request_message_ptr: *const u8;
    let request_message_size: usize;
    let reply_message_ptr: *const u8;
    let reply_message_size: usize;

    unsafe {
        core::arch::asm!(
            "mov x1, {name_len}",
            "mov x0, {name_ptr}",
            "mov x8, #7",  // #7: KERNEL_CALL_RECEIVE_REQUEST
            "svc #0x51",
            name_len = in(reg) server_name.len(),
            name_ptr = in(reg) server_name.as_ptr(),
            out("x0") client_thread,
            out("x1") request_message_type,
            out("x2") request_message_ptr,
            out("x3") request_message_size,
            out("x4") reply_message_ptr,
            out("x5") reply_message_size,
            clobber_abi("C"),
            options(nostack, preserves_flags),
        );
    }

    if client_thread == core::ptr::null() {
        Err(crate::error_kind::ErrorKind::Other.into())
    } else {
        Ok((client_thread as u64, request_message_type, request_message_ptr, request_message_size, reply_message_ptr, reply_message_size))
    }
}

#[inline(never)]
pub fn syscall_create_server(server_name: &str) -> i32 {
    let mut ret: i32 = 0;

    unsafe {
        core::arch::asm!(
            "mov x1, {name_len}",
            "mov x0, {name_ptr}",
            "mov x8, #8",  // #8: KERNEL_CALL_CREATE_SERVER
            "svc #0x51",
            name_len = in(reg) server_name.len(),
            name_ptr = in(reg) server_name.as_ptr(),
            out("x0") ret,
            clobber_abi("C"),
            options(nostack, preserves_flags),
        );
    }

    ret
}

#[inline(never)]
pub fn syscall_reply_to_request(server_name: &str, client_thraed: u64, reply_message: i64) -> Result<(), crate::error_kind::ErrorKind>{
    let mut result: i32 = 0;

    unsafe {
        core::arch::asm!(
            "mov x3, {reply_message}",
            "mov x2, {client_thread}",
            "mov x1, {name_len}",
            "mov x0, {name_ptr}",
            "mov x8, #9",  // #9: KERNEL_CALL_RESPOND_TO_REQUEST
            "svc #0x51",
            reply_message = in(reg) reply_message,
            client_thread = in(reg) client_thraed,
            name_len = in(reg) server_name.len(),
            name_ptr = in(reg) server_name.as_ptr(),
            out("x0") result,
            clobber_abi("C"),
            options(nostack, preserves_flags),
        );
    }

    if result < 0 {
        Err(crate::error_kind::ErrorKind::Other.into())
    } else {
        Ok(())
    }
}

pub fn syscall_reply_to_service_request(server_name: &str, request_client_thread_id: u64, reply_message_ptr: *const u8, reply_message_size: usize, reply_message_error: i64) -> Result<(), crate::error_kind::ErrorKind>{
    let mut result: i64 = 0;

    unsafe {
        core::arch::asm!(
            "mov x5, {reply_message_error}",
            "mov x4, {reply_message_size}",
            "mov x3, {reply_message_ptr}",
            "mov x2, {request_client_thread_id}",
            "mov x1, {name_len}",
            "mov x0, {name_ptr}",
            "mov x8, #12",  // #12: KERNEL_CALL_REPLY_TO_SERVICE_REQUEST
            "svc #0x51",
            reply_message_error = in(reg) reply_message_error,
            reply_message_size = in(reg) reply_message_size,
            reply_message_ptr = in(reg) reply_message_ptr,
            request_client_thread_id = in(reg) request_client_thread_id,
            name_len = in(reg) server_name.len(),
            name_ptr = in(reg) server_name.as_ptr(),
            out("x0") result,
            clobber_abi("C"),
            options(nostack, preserves_flags),
        );
    }

    if result < 0 {
        Err(crate::error_kind::ErrorKind::Other.into())
    } else {
        Ok(())
    }
}

#[inline(never)]
pub fn syscall_sleep(secs: u64, nanos: u32) {
    unsafe {
        core::arch::asm!(
            "mov x0, {secs}",
            "mov x1, {nanos}",
            "mov x8, #14",  // #14: KERNEL_CALL_SLEEP
            "svc #0x51",
            secs = in(reg) secs,
            nanos = in(reg) nanos,
            clobber_abi("C"),
            options(nostack, preserves_flags),
        );
    }
}

/// Creates a new topic with zero-copy support.
///
/// This creates a topic and sets up shared memory for zero-copy operations.
/// The shared memory buffer is mapped into the caller's address space.
///
/// # Arguments
///
/// * `topic` - The name of the topic to create
///
/// # Register Layout
///
/// - x0: topic name pointer
/// - x1: topic name length
/// - x8: syscall number (#15: KERNEL_CALL_CREATE_TOPIC)
#[inline(never)]
pub fn syscall_create_topic(topic: &str) {
    unsafe {
        core::arch::asm!(
            "mov x1, {topic_len}",
            "mov x0, {topic_ptr}",
            "mov x8, #15",  // #15: KERNEL_CALL_CREATE_TOPIC
            "svc #0x51",
            topic_len = in(reg) topic.len(),
            topic_ptr = in(reg) topic.as_ptr(),
            clobber_abi("C"),
            options(nostack, preserves_flags),
        );
    }
}

/// Gets the shared memory buffer for a topic (zero-copy).
///
/// Returns a pointer to the shared memory buffer that's already mapped
/// into the caller's address space. This enables true zero-copy operations.
///
/// # Arguments
///
/// * `topic` - The name of the topic
///
/// # Returns
///
/// A tuple of (buffer_pointer, buffer_size)
///
/// # Register Layout
///
/// - x0: topic name pointer
/// - x1: topic name length
/// - x8: syscall number (#16: KERNEL_CALL_GET_TOPIC_SHARED_MEMORY)
/// - Returns: x0 = buffer pointer, x1 = buffer size
#[inline(never)]
pub fn syscall_get_topic_shared_memory(topic: &str) -> (*mut u8, usize) {
    let mut buffer_ptr: *mut u8;
    let mut buffer_size: usize;
    unsafe {
        core::arch::asm!(
            "mov x1, {topic_len}",
            "mov x0, {topic_ptr}",
            "mov x8, #16",  // #16: KERNEL_CALL_GET_TOPIC_SHARED_MEMORY
            "svc #0x51",
            topic_len = in(reg) topic.len(),
            topic_ptr = in(reg) topic.as_ptr(),
            out("x0") buffer_ptr,
            out("x1") buffer_size,
            clobber_abi("C"),
            options(nostack, preserves_flags),
        );
    }
    (buffer_ptr, buffer_size)
}

/// Commits a publish after writing to shared memory (zero-copy).
///
/// This is a lightweight kernel call that sets the flag and wakes subscribers.
/// No data is copied - the data is already in shared memory.
///
/// # Arguments
///
/// * `topic` - The name of the topic
/// * `message_size` - The size of the message written to shared memory
///
/// # Register Layout
///
/// - x0: topic name pointer
/// - x1: topic name length
/// - x2: message size
/// - x8: syscall number (#17: KERNEL_CALL_COMMIT_PUBLISH)
#[inline(never)]
pub fn syscall_commit_publish(topic: &str, message_size: usize) {
    unsafe {
        core::arch::asm!(
            "mov x2, {message_size}",
            "mov x1, {topic_len}",
            "mov x0, {topic_ptr}",
            "mov x8, #17",  // #17: KERNEL_CALL_COMMIT_PUBLISH
            "svc #0x51",
            message_size = in(reg) message_size,
            topic_len = in(reg) topic.len(),
            topic_ptr = in(reg) topic.as_ptr(),
            clobber_abi("C"),
            options(nostack, preserves_flags),
        );
    }
}

/// Subscribes to a topic with zero-copy support.
///
/// This blocks until a message is available, then returns a pointer to
/// the message in shared memory. No data is copied.
///
/// # Arguments
///
/// * `topic` - The name of the topic to subscribe to
///
/// # Returns
///
/// A tuple of (message_pointer, message_size)
///
/// # Register Layout
///
/// - x0: topic name pointer
/// - x1: topic name length
/// - x8: syscall number (#18: KERNEL_CALL_SUBSCRIBE_TOPIC_ZERO_COPY)
/// - Returns: x0 = message pointer, x1 = message size
#[inline(never)]
pub fn syscall_subscribe_topic_zero_copy(topic: &str) -> (*const u8, usize) {
    let mut message_ptr: *const u8;
    let mut message_size: usize;
    unsafe {
        core::arch::asm!(
            "mov x1, {topic_len}",
            "mov x0, {topic_ptr}",
            "mov x8, #18",  // #18: KERNEL_CALL_SUBSCRIBE_TOPIC_ZERO_COPY
            "svc #0x51",
            topic_len = in(reg) topic.len(),
            topic_ptr = in(reg) topic.as_ptr(),
            out("x0") message_ptr,
            out("x1") message_size,
            clobber_abi("C"),
            options(nostack, preserves_flags),
        );
    }
    (message_ptr, message_size)
}

/// Acknowledges that a message has been read (zero-copy).
///
/// This allows the publisher to write the next message. Call this after
/// you've finished reading the current message.
///
/// # Arguments
///
/// * `topic` - The name of the topic
///
/// # Register Layout
///
/// - x0: topic name pointer
/// - x1: topic name length
/// - x8: syscall number (#19: KERNEL_CALL_ACK_MESSAGE)
#[inline(never)]
pub fn syscall_ack_message(topic: &str) {
    unsafe {
        core::arch::asm!(
            "mov x1, {topic_len}",
            "mov x0, {topic_ptr}",
            "mov x8, #19",  // #19: KERNEL_CALL_ACK_MESSAGE
            "svc #0x51",
            topic_len = in(reg) topic.len(),
            topic_ptr = in(reg) topic.as_ptr(),
            clobber_abi("C"),
            options(nostack, preserves_flags),
        );
    }
}