Files
Deshab-OS/CODE/UTSM/utrw/read.c
T
xyou f1a2a110d7 Record networking and DMA development progress
Add the current OS source tree, build scripts, system image layout, and development notes covering DMA page allocation, e1000 bring-up, net API dispatch, and DHCP work in progress.
2026-07-02 16:17:09 +08:00

57 lines
1.6 KiB
C

#include <utsm/utrw.h>
#include <utsm/segment.h>
#include <utsm/pckc.h>
#include <utsm/crypto.h>
#include <utsm/config.h>
void *memcpy(void *dst, const void *src, usize len);
int utsm_read(utsm_capability cap, u64 offset, void *dst, u64 len) {
if (!dst && len != 0) {
return UTSM_ERR_INVALID;
}
if (len == 0) {
return UTSM_OK;
}
utsm_segment_desc *desc = NULL;
int status = utsm_cap_validate(cap, UTSM_RIGHT_READ, offset, len, &desc);
if (status != UTSM_OK) {
return status;
}
utsm_key_material key;
status = utsm_pckc_get_or_derive(cap.segment_slot, desc, &key);
if (status != UTSM_OK) {
return status;
}
u8 line_plain[UTSM_CACHE_LINE_SIZE];
u64 seq_before = desc->writer_seq;
if (seq_before & 1ULL) {
return UTSM_ERR_RETRY;
}
u64 copied = 0;
while (copied < len) {
u64 absolute = offset + copied;
u64 line_start = (absolute / UTSM_CACHE_LINE_SIZE) * UTSM_CACHE_LINE_SIZE;
u64 in_line = absolute - line_start;
u64 chunk = UTSM_CACHE_LINE_SIZE - in_line;
if (chunk > len - copied) {
chunk = len - copied;
}
u64 line_index = line_start / UTSM_CACHE_LINE_SIZE;
u64 tweak = utsm_make_tweak(desc->tweak_seed, line_index, desc->key_epoch);
utsm_crypt_line(&key, tweak, desc->cipher_base + line_start, line_plain);
memcpy((u8 *)dst + copied, line_plain + in_line, chunk);
copied += chunk;
}
u64 seq_after = desc->writer_seq;
if (seq_before != seq_after) {
return UTSM_ERR_RETRY;
}
return UTSM_OK;
}