ruvix-boot 0.1.0

RVF boot loading for RuVix Cognition Kernel (ADR-087)
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
//! RVF package mounting (rvf_mount syscall implementation).
//!
//! The rvf_mount operation:
//! 1. Verifies package signature
//! 2. Parses manifest
//! 3. Creates regions per memory schema
//! 4. Mounts WASM components
//! 5. Distributes capabilities per manifest
//! 6. Connects queues per wiring
//! 7. Spawns initial tasks per WIT entry points

use crate::manifest::RvfManifest;
use crate::signature::SignatureVerifier;
use ruvix_types::{
    KernelError, RvfMountHandle, RvfVerifyStatus, RegionHandle,
    TaskHandle, TaskPriority,
};

#[cfg(feature = "alloc")]
use alloc::vec::Vec;

/// Configuration for RVF mount operations.
#[derive(Debug, Clone)]
pub struct MountConfig {
    /// Maximum components per package.
    pub max_components: usize,

    /// Maximum regions per package.
    pub max_regions: usize,

    /// Maximum queues per package.
    pub max_queues: usize,

    /// Default task priority for spawned tasks.
    pub default_task_priority: TaskPriority,

    /// Whether to verify signatures (should always be true in production).
    pub verify_signatures: bool,
}

impl Default for MountConfig {
    fn default() -> Self {
        Self {
            max_components: 256,
            max_regions: 256,
            max_queues: 1024,
            default_task_priority: TaskPriority::Normal,
            verify_signatures: true,
        }
    }
}

/// Result of an RVF mount operation.
#[derive(Debug, Clone)]
pub struct MountResult {
    /// Handle to the mounted RVF package.
    pub mount_handle: RvfMountHandle,

    /// Verification status.
    pub verify_status: RvfVerifyStatus,

    /// Number of components mounted.
    pub components_mounted: usize,

    /// Number of regions created.
    pub regions_created: usize,

    /// Number of queues connected.
    pub queues_connected: usize,

    /// Number of tasks spawned.
    pub tasks_spawned: usize,

    /// Created region handles.
    pub region_handles: [Option<RegionHandle>; 256],
    /// Number of region handles in the array.
    pub region_handle_count: usize,

    /// Spawned task handles.
    pub task_handles: [Option<TaskHandle>; 64],
    /// Number of task handles in the array.
    pub task_handle_count: usize,
}

impl MountResult {
    /// Creates a successful mount result.
    #[must_use]
    pub fn success(mount_handle: RvfMountHandle) -> Self {
        Self {
            mount_handle,
            verify_status: RvfVerifyStatus::SignatureValid,
            components_mounted: 0,
            regions_created: 0,
            queues_connected: 0,
            tasks_spawned: 0,
            region_handles: [None; 256],
            region_handle_count: 0,
            task_handles: [None; 64],
            task_handle_count: 0,
        }
    }

    /// Creates a failed mount result.
    #[must_use]
    pub fn failure(status: RvfVerifyStatus) -> Self {
        Self {
            mount_handle: RvfMountHandle::null(),
            verify_status: status,
            components_mounted: 0,
            regions_created: 0,
            queues_connected: 0,
            tasks_spawned: 0,
            region_handles: [None; 256],
            region_handle_count: 0,
            task_handles: [None; 64],
            task_handle_count: 0,
        }
    }

    /// Adds a created region handle.
    pub fn add_region(&mut self, handle: RegionHandle) -> Result<(), KernelError> {
        if self.region_handle_count >= 256 {
            return Err(KernelError::LimitExceeded);
        }
        self.region_handles[self.region_handle_count] = Some(handle);
        self.region_handle_count += 1;
        self.regions_created += 1;
        Ok(())
    }

    /// Adds a spawned task handle.
    pub fn add_task(&mut self, handle: TaskHandle) -> Result<(), KernelError> {
        if self.task_handle_count >= 64 {
            return Err(KernelError::LimitExceeded);
        }
        self.task_handles[self.task_handle_count] = Some(handle);
        self.task_handle_count += 1;
        self.tasks_spawned += 1;
        Ok(())
    }
}

/// RVF mount operation handler.
///
/// Implements the rvf_mount syscall per ADR-087.
pub struct RvfMount {
    /// Configuration.
    config: MountConfig,

    /// Signature verifier.
    verifier: Option<SignatureVerifier>,

    /// Next mount handle ID.
    next_mount_id: u32,

    /// Next region handle ID.
    next_region_id: u32,

    /// Next task handle ID.
    next_task_id: u32,
}

impl RvfMount {
    /// Creates a new RVF mount handler.
    #[must_use]
    pub fn new(config: MountConfig) -> Self {
        Self {
            config,
            verifier: None,
            next_mount_id: 1,
            next_region_id: 0x1000,
            next_task_id: 0x100,
        }
    }

    /// Sets the signature verification public key.
    pub fn set_public_key(&mut self, public_key: &[u8]) {
        self.verifier = Some(SignatureVerifier::new(public_key));
    }

    /// Mounts an RVF package.
    ///
    /// # Steps
    ///
    /// 1. Verify package signature
    /// 2. Parse manifest
    /// 3. Create regions per memory schema
    /// 4. Mount WASM components
    /// 5. Distribute capabilities per manifest
    /// 6. Connect queues per wiring
    /// 7. Spawn initial tasks per WIT entry points
    ///
    /// # Errors
    ///
    /// - `InvalidSignature` if signature verification fails
    /// - `InvalidManifest` if manifest parsing fails
    /// - `OutOfMemory` if region creation fails
    /// - `LimitExceeded` if max components/regions/queues exceeded
    pub fn mount(
        &mut self,
        manifest_bytes: &[u8],
        signature: &[u8],
        _package_bytes: &[u8], // WASM components - not used in Phase A
    ) -> Result<MountResult, KernelError> {
        // Step 1: Verify signature
        if self.config.verify_signatures {
            self.verify_signature(manifest_bytes, signature)?;
        }

        // Step 2: Parse manifest
        let manifest = RvfManifest::parse(manifest_bytes)?;

        // Validate manifest
        if !manifest.validate() {
            return Ok(MountResult::failure(RvfVerifyStatus::ManifestInvalid));
        }

        // Check limits
        if manifest.component_graph.component_count() > self.config.max_components {
            return Err(KernelError::LimitExceeded);
        }
        if manifest.memory_schema.region_count() > self.config.max_regions {
            return Err(KernelError::LimitExceeded);
        }
        if manifest.component_graph.wiring_count() > self.config.max_queues {
            return Err(KernelError::LimitExceeded);
        }

        // Allocate mount handle
        let mount_handle = RvfMountHandle::new(self.next_mount_id, 0);
        self.next_mount_id += 1;

        let mut result = MountResult::success(mount_handle);

        // Step 3: Create regions
        self.create_regions(&manifest, &mut result)?;

        // Step 4: Mount components (Phase A: mock)
        result.components_mounted = manifest.component_graph.component_count();

        // Step 5: Distribute capabilities (Phase A: mock)
        // In production, this creates actual capability entries

        // Step 6: Connect queues (Phase A: mock)
        result.queues_connected = manifest.component_graph.wiring_count();

        // Step 7: Spawn tasks
        self.spawn_initial_tasks(&manifest, &mut result)?;

        Ok(result)
    }

    fn verify_signature(&self, manifest: &[u8], signature: &[u8]) -> Result<(), KernelError> {
        match &self.verifier {
            Some(verifier) => {
                let result = verifier.verify(manifest, signature);
                if result.is_valid() {
                    Ok(())
                } else {
                    Err(KernelError::InvalidSignature)
                }
            }
            None => {
                // No verifier configured - require signature verification
                Err(KernelError::InternalError)
            }
        }
    }

    fn create_regions(
        &mut self,
        manifest: &RvfManifest,
        result: &mut MountResult,
    ) -> Result<(), KernelError> {
        let region_count = manifest.memory_schema.region_count();

        for _ in 0..region_count {
            let handle = RegionHandle::new(self.next_region_id, 0);
            self.next_region_id += 1;
            result.add_region(handle)?;
        }

        Ok(())
    }

    fn spawn_initial_tasks(
        &mut self,
        manifest: &RvfManifest,
        result: &mut MountResult,
    ) -> Result<(), KernelError> {
        let component_count = manifest.component_graph.component_count();

        // In production, we would check each component's entry point
        // and spawn a task if it has one. For Phase A, spawn one task
        // per component with an entry point.

        // Phase A: Spawn one task for the root component
        if component_count > 0 {
            let handle = TaskHandle::new(self.next_task_id, 0);
            self.next_task_id += 1;
            result.add_task(handle)?;
        }

        Ok(())
    }

    /// Unmounts an RVF package.
    ///
    /// # Steps
    ///
    /// 1. Stop all tasks
    /// 2. Disconnect queues
    /// 3. Revoke capabilities
    /// 4. Destroy regions
    /// 5. Free mount handle
    pub fn unmount(&mut self, _handle: RvfMountHandle) -> Result<(), KernelError> {
        // Phase A: Mock implementation
        // In production, this would perform the full unmount sequence
        Ok(())
    }
}

impl Default for RvfMount {
    fn default() -> Self {
        Self::new(MountConfig::default())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::signature::SIGNATURE_SIZE;

    fn create_test_manifest() -> Vec<u8> {
        let mut manifest = vec![0u8; 100];
        manifest[0..4].copy_from_slice(b"RVF1");
        manifest[4..6].copy_from_slice(&1u16.to_le_bytes()); // major
        manifest[6..8].copy_from_slice(&0u16.to_le_bytes()); // minor
        manifest
    }

    fn create_test_signature(manifest: &[u8]) -> Vec<u8> {
        use sha2::{Sha256, Digest};

        let mut sig = vec![0u8; SIGNATURE_SIZE];
        sig[0..4].copy_from_slice(b"TEST");

        let mut hasher = Sha256::new();
        hasher.update(manifest);
        let hash = hasher.finalize();
        sig[4..36].copy_from_slice(&hash);

        sig
    }

    #[test]
    fn test_mount_config_default() {
        let config = MountConfig::default();
        assert_eq!(config.max_components, 256);
        assert_eq!(config.max_regions, 256);
        assert!(config.verify_signatures);
    }

    #[test]
    fn test_mount_result_success() {
        let handle = RvfMountHandle::new(1, 0);
        let result = MountResult::success(handle);

        assert_eq!(result.verify_status, RvfVerifyStatus::SignatureValid);
        assert!(!result.mount_handle.is_null());
    }

    #[test]
    fn test_mount_result_add_region() {
        let mut result = MountResult::success(RvfMountHandle::new(1, 0));

        result.add_region(RegionHandle::new(1, 0)).unwrap();
        result.add_region(RegionHandle::new(2, 0)).unwrap();

        assert_eq!(result.regions_created, 2);
        assert_eq!(result.region_handle_count, 2);
    }

    #[test]
    fn test_mount_result_add_task() {
        let mut result = MountResult::success(RvfMountHandle::new(1, 0));

        result.add_task(TaskHandle::new(1, 0)).unwrap();

        assert_eq!(result.tasks_spawned, 1);
        assert_eq!(result.task_handle_count, 1);
    }

    #[test]
    fn test_rvf_mount_basic() {
        let mut mount = RvfMount::new(MountConfig::default());
        mount.set_public_key(&[0u8; 1952]); // Test key

        let manifest = create_test_manifest();
        let signature = create_test_signature(&manifest);

        let result = mount.mount(&manifest, &signature, &[]).unwrap();

        assert_eq!(result.verify_status, RvfVerifyStatus::SignatureValid);
        assert!(!result.mount_handle.is_null());
    }

    #[test]
    fn test_rvf_mount_invalid_signature() {
        let mut mount = RvfMount::new(MountConfig::default());
        mount.set_public_key(&[0u8; 1952]);

        let manifest = create_test_manifest();
        let wrong_manifest = b"wrong manifest";
        let signature = create_test_signature(wrong_manifest);

        let result = mount.mount(&manifest, &signature, &[]);

        assert!(result.is_err());
        assert_eq!(result.unwrap_err(), KernelError::InvalidSignature);
    }

    #[test]
    fn test_rvf_mount_skip_verification() {
        let mut config = MountConfig::default();
        config.verify_signatures = false;

        let mut mount = RvfMount::new(config);

        let manifest = create_test_manifest();
        let bad_signature = [0u8; 100]; // Wrong size, but we skip verification

        let result = mount.mount(&manifest, &bad_signature, &[]).unwrap();

        assert_eq!(result.verify_status, RvfVerifyStatus::SignatureValid);
    }

    #[test]
    fn test_rvf_unmount() {
        let mut mount = RvfMount::default();
        let handle = RvfMountHandle::new(1, 0);

        // Should not fail
        mount.unmount(handle).unwrap();
    }
}