interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core-mv / crates / aien-platform-linux / src / buffer.rs

184 lines · 5607 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Linux Unified Buffer implementation for Grace Blackwell GB10 and Linux host.
17//! Allocates anonymous page-aligned mmap; GB10 shares one coherent system memory pool (ATS).
18
19use aien_platform::{BufferLayout, DeviceAddress, PlatformError, UnifiedBuffer};
20use core::mem::align_of;
21use core::ptr::NonNull;
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub enum LinuxMemoryKind {
25 AtsSystem,
26 HmmSystem,
27 /// Memory another allocator owns (the GPU engine's uncached, host-visible pool): released
28 /// through its callback when the buffer drops, never unmapped here.
29 External,
30}
31
32#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33pub enum ResidencyPolicy {
34 Pageable,
35 FaultIn,
36 Locked,
37}
38
39pub struct LinuxUnifiedBuffer {
40 ptr: NonNull<u8>,
41 mapping_base: NonNull<u8>,
42 mapping_len: usize,
43 len: usize,
44 align: usize,
45 kind: LinuxMemoryKind,
46 device_address: DeviceAddress,
47 /// `External` memory: runs once on drop instead of the unmap.
48 release: Option<Box<dyn FnOnce() + Send + Sync>>,
49}
50
51unsafe impl Send for LinuxUnifiedBuffer {}
52unsafe impl Sync for LinuxUnifiedBuffer {}
53
54impl LinuxUnifiedBuffer {
55 pub fn allocate(
56 layout: BufferLayout,
57 kind: LinuxMemoryKind,
58 policy: ResidencyPolicy,
59 ) -> Result<Self, PlatformError> {
60 if layout.size == 0 {
61 return Err(PlatformError::InvalidLayout);
62 }
63
64 let align = layout.align.max(align_of::<usize>()).max(64);
65 let mapping_len = layout
66 .size
67 .checked_add(align)
68 .ok_or(PlatformError::OutOfMemory)?;
69
70 let mapping_base_ptr = unsafe {
71 libc::mmap(
72 core::ptr::null_mut(),
73 mapping_len,
74 libc::PROT_READ | libc::PROT_WRITE,
75 libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
76 -1,
77 0,
78 )
79 };
80 if mapping_base_ptr == libc::MAP_FAILED || mapping_base_ptr.is_null() {
81 return Err(PlatformError::OutOfMemory);
82 }
83 let mapping_base_ptr = mapping_base_ptr as *mut u8;
84 let actual_kind = kind;
85
86 let mapping_base = NonNull::new(mapping_base_ptr).ok_or(PlatformError::OutOfMemory)?;
87 let base_addr = mapping_base_ptr as usize;
88 let aligned_addr = (base_addr + align - 1) & !(align - 1);
89 let ptr = NonNull::new(aligned_addr as *mut u8).ok_or(PlatformError::OutOfMemory)?;
90
91 if policy == ResidencyPolicy::FaultIn {
92 let page_size = 4096;
93 for offset in (0..layout.size).step_by(page_size) {
94 unsafe {
95 core::ptr::write_volatile(ptr.as_ptr().add(offset), 0);
96 }
97 }
98 }
99
100 let device_address = DeviceAddress(aligned_addr as u64);
101
102 Ok(Self {
103 ptr,
104 mapping_base,
105 mapping_len,
106 len: layout.size,
107 align,
108 kind: actual_kind,
109 device_address,
110 release: None,
111 })
112 }
113
114 /// A buffer over memory another allocator owns (`LinuxMemoryKind::External`): `len` bytes at
115 /// `ptr`, reachable by the device at `device_address`. `release` runs exactly once, when the
116 /// buffer drops; nothing is unmapped here.
117 ///
118 /// # Safety
119 /// `ptr` must be non-null and stay valid, readable and writable for `len` bytes until
120 /// `release` runs, and nothing else may free it.
121 pub unsafe fn from_external(
122 ptr: *mut u8,
123 len: usize,
124 device_address: DeviceAddress,
125 release: Box<dyn FnOnce() + Send + Sync>,
126 ) -> Result<Self, PlatformError> {
127 if len == 0 {
128 return Err(PlatformError::InvalidLayout);
129 }
130 let ptr = NonNull::new(ptr).ok_or(PlatformError::InvalidLayout)?;
131 let align = 1usize << (ptr.as_ptr() as usize).trailing_zeros().min(12);
132 Ok(Self {
133 ptr,
134 mapping_base: ptr,
135 mapping_len: len,
136 len,
137 align,
138 kind: LinuxMemoryKind::External,
139 device_address,
140 release: Some(release),
141 })
142 }
143
144 pub fn kind(&self) -> LinuxMemoryKind {
145 self.kind
146 }
147
148 pub fn alignment(&self) -> usize {
149 self.align
150 }
151}
152
153impl UnifiedBuffer for LinuxUnifiedBuffer {
154 fn len(&self) -> usize {
155 self.len
156 }
157
158 fn device_address(&self) -> DeviceAddress {
159 self.device_address
160 }
161
162 fn as_ptr(&self) -> *const u8 {
163 self.ptr.as_ptr()
164 }
165
166 fn as_mut_ptr(&mut self) -> *mut u8 {
167 self.ptr.as_ptr()
168 }
169}
170
171impl Drop for LinuxUnifiedBuffer {
172 fn drop(&mut self) {
173 if let Some(release) = self.release.take() {
174 release();
175 return;
176 }
177 unsafe {
178 libc::munmap(
179 self.mapping_base.as_ptr() as *mut libc::c_void,
180 self.mapping_len,
181 );
182 }
183 }
184}