libhermit-rs 0.6.3

A Rust-based library 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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//! A module containing all virtio specific pci functionality
//!
//! The module contains ...
#![allow(dead_code)]

use core::convert::TryInto;
use core::intrinsics::unaligned_volatile_store;
use core::ptr::{read_volatile, write_volatile};
use core::result::Result;
use core::sync::atomic::{fence, Ordering};
use core::u8;

#[cfg(feature = "tcp")]
use crate::arch::kernel::interrupts::*;
use crate::arch::mm::PhysAddr;
use crate::drivers::error::DriverError;
#[cfg(feature = "tcp")]
use crate::drivers::net::network_irqhandler;
#[cfg(feature = "tcp")]
use crate::drivers::net::virtio_net::VirtioNetDriver;
use crate::drivers::virtio::device;
use crate::drivers::virtio::error::VirtioError;

/// Virtio device ID's
/// See Virtio specification v1.1. - 5
///
// WARN: Upon changes in the set of the enum variants
// one MUST adjust the associated From<u32>
// implementation, in order catch all cases correctly,
// as this function uses the catch-all "_" case!
#[allow(non_camel_case_types, clippy::upper_case_acronyms)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
#[repr(u32)]
pub enum DevId {
	INVALID = 0x0,
	VIRTIO_DEV_ID_NET = 1,
	VIRTIO_DEV_ID_BLK = 2,
	VIRTIO_DEV_ID_CONSOLE = 3,
}

impl From<DevId> for u32 {
	fn from(val: DevId) -> u32 {
		match val {
			DevId::VIRTIO_DEV_ID_NET => 1,
			DevId::VIRTIO_DEV_ID_BLK => 2,
			DevId::VIRTIO_DEV_ID_CONSOLE => 3,
			DevId::INVALID => 0x0,
		}
	}
}

impl From<u32> for DevId {
	fn from(val: u32) -> Self {
		match val {
			1 => DevId::VIRTIO_DEV_ID_NET,
			2 => DevId::VIRTIO_DEV_ID_BLK,
			3 => DevId::VIRTIO_DEV_ID_CONSOLE,
			_ => DevId::INVALID,
		}
	}
}

pub struct VqCfgHandler<'a> {
	vq_index: u32,
	raw: &'a mut MmioRegisterLayout,
}

impl<'a> VqCfgHandler<'a> {
	/// Sets the size of a given virtqueue. In case the provided size exceeds the maximum allowed
	/// size, the size is set to this maximum instead. Else size is set to the provided value.
	///
	/// Returns the set size in form of a `u16`.
	pub fn set_vq_size(&mut self, size: u16) -> u16 {
		self.raw
			.set_queue_size(self.vq_index, size as u32)
			.try_into()
			.unwrap()
	}

	pub fn set_ring_addr(&mut self, addr: PhysAddr) {
		self.raw.set_ring_addr(self.vq_index, addr);
	}

	pub fn set_drv_ctrl_addr(&mut self, addr: PhysAddr) {
		self.raw.set_drv_ctrl_addr(self.vq_index, addr);
	}

	pub fn set_dev_ctrl_addr(&mut self, addr: PhysAddr) {
		self.raw.set_dev_ctrl_addr(self.vq_index, addr);
	}

	pub fn notif_off(&mut self) -> u16 {
		// we don't need an offset
		0
	}

	pub fn enable_queue(&mut self) {
		self.raw.enable_queue(self.vq_index);
	}
}

/// Wraps a [ComCfgRaw](structs.comcfgraw.html) in order to preserve
/// the original structure.
///
/// Provides a safe API for Raw structure and allows interaction with the device via
/// the structure.
pub struct ComCfg {
	// References the raw structure in PCI memory space. Is static as
	// long as the device is present, which is mandatory in order to let this code work.
	com_cfg: &'static mut MmioRegisterLayout,

	/// Preferences of the device for this config. From 1 (highest) to 2^7-1 (lowest)
	rank: u8,
}

// Public Interface of ComCfg
impl ComCfg {
	pub fn new(raw: &'static mut MmioRegisterLayout, rank: u8) -> Self {
		ComCfg { com_cfg: raw, rank }
	}

	/// Select a queue via an index. If queue does NOT exist returns `None`, else
	/// returns `Some(VqCfgHandler)`.
	///
	/// INFO: The queue size is automatically bounded by constant `src::config:VIRTIO_MAX_QUEUE_SIZE`.
	pub fn select_vq(&mut self, index: u16) -> Option<VqCfgHandler<'_>> {
		if self.com_cfg.get_max_queue_size(u32::from(index)) == 0 {
			None
		} else {
			Some(VqCfgHandler {
				vq_index: index as u32,
				raw: self.com_cfg,
			})
		}
	}

	pub fn get_max_queue_size(&mut self, sel: u32) -> u32 {
		self.com_cfg.get_max_queue_size(sel)
	}

	pub fn get_queue_ready(&mut self, sel: u32) -> bool {
		self.com_cfg.get_queue_ready(sel)
	}

	/// Returns the device status field.
	pub fn dev_status(&self) -> u8 {
		unsafe { read_volatile(&self.com_cfg.status).try_into().unwrap() }
	}

	/// Resets the device status field to zero.
	pub fn reset_dev(&mut self) {
		unsafe {
			write_volatile(&mut self.com_cfg.status, 0u32);
		}
	}

	/// Sets the device status field to FAILED.
	/// A driver MUST NOT initialize and use the device any further after this.
	/// A driver MAY use the device again after a proper reset of the device.
	pub fn set_failed(&mut self) {
		unsafe {
			write_volatile(&mut self.com_cfg.status, u32::from(device::Status::FAILED));
		}
	}

	/// Sets the ACKNOWLEDGE bit in the device status field. This indicates, the
	/// OS has notived the device
	pub fn ack_dev(&mut self) {
		unsafe {
			let status = read_volatile(&self.com_cfg.status);
			write_volatile(
				&mut self.com_cfg.status,
				status | u32::from(device::Status::ACKNOWLEDGE),
			);
		}
	}

	/// Sets the DRIVER bit in the device status field. This indicates, the OS
	/// know how to run this device.
	pub fn set_drv(&mut self) {
		unsafe {
			let status = read_volatile(&self.com_cfg.status);
			write_volatile(
				&mut self.com_cfg.status,
				status | u32::from(device::Status::DRIVER),
			);
		}
	}

	/// Sets the FEATURES_OK bit in the device status field.
	///
	/// Drivers MUST NOT accept new features after this step.
	pub fn features_ok(&mut self) {
		unsafe {
			let status = read_volatile(&self.com_cfg.status);
			write_volatile(
				&mut self.com_cfg.status,
				status | u32::from(device::Status::FEATURES_OK),
			);
		}
	}

	/// In order to correctly check feature negotiaten, this function
	/// MUST be called after [self.features_ok()](ComCfg::features_ok()) in order to check
	/// if features have been accepted by the device after negotiation.
	///
	/// Re-reads device status to ensure the FEATURES_OK bit is still set:
	/// otherwise, the device does not support our subset of features and the device is unusable.
	pub fn check_features(&self) -> bool {
		unsafe {
			let status = read_volatile(&self.com_cfg.status);
			status & u32::from(device::Status::FEATURES_OK)
				== u32::from(device::Status::FEATURES_OK)
		}
	}

	/// Sets the DRIVER_OK bit in the device status field.
	///
	/// After this call, the device is "live"!
	pub fn drv_ok(&mut self) {
		unsafe {
			let status = read_volatile(&self.com_cfg.status);
			write_volatile(
				&mut self.com_cfg.status,
				status | u32::from(device::Status::DRIVER_OK),
			);
		}
	}

	/// Returns the features offered by the device. Coded in a 64bit value.
	pub fn dev_features(&mut self) -> u64 {
		self.com_cfg.dev_features()
	}

	/// Write selected features into driver_select field.
	pub fn set_drv_features(&mut self, feats: u64) {
		self.com_cfg.set_drv_features(feats);
	}

	pub fn print_information(&mut self) {
		self.com_cfg.print_information();
	}
}

/// Notification Structure to handle virtqueue notification settings.
/// See Virtio specification v1.1 - 4.1.4.4
pub struct NotifCfg {
	/// Start addr, from where the notification addresses for the virtqueues are computed
	queue_notify: *mut u32,
}

impl NotifCfg {
	pub fn new(registers: &mut MmioRegisterLayout) -> Self {
		let raw = &mut registers.queue_notify as *mut u32;

		NotifCfg { queue_notify: raw }
	}

	/// Returns base address of notification area as an usize
	pub fn base(&self) -> usize {
		self.queue_notify as usize
	}

	/// Returns the multiplier, needed in order to calculate the
	/// notification address for a specific queue.
	pub fn multiplier(&self) -> u32 {
		// we don't need a multiplier
		0
	}
}

/// Control structure, allowing to notify a device via PCI bus.
/// Typically hold by a virtqueue.
pub struct NotifCtrl {
	/// Indicates if VIRTIO_F_NOTIFICATION_DATA has been negotiated
	f_notif_data: bool,
	/// Where to write notification
	notif_addr: *mut u32,
}

impl NotifCtrl {
	/// Returns a new controller. By default MSI-X capabilities and VIRTIO_F_NOTIFICATION_DATA
	/// are disabled.
	pub fn new(notif_addr: *mut usize) -> Self {
		NotifCtrl {
			f_notif_data: false,
			notif_addr: notif_addr as *mut u32,
		}
	}

	/// Enables VIRTIO_F_NOTIFICATION_DATA. This changes which data is provided to the device. ONLY a good idea if Feature has been negotiated.
	pub fn enable_notif_data(&mut self) {
		self.f_notif_data = true;
	}

	pub fn notify_dev(&self, notif_data: &[u8]) {
		fence(Ordering::Acquire);

		if self.f_notif_data {
			let ptr = self.notif_addr;

			unsafe {
				unaligned_volatile_store(
					ptr,
					u32::from_ne_bytes(notif_data[0..4].try_into().unwrap()),
				);
			}
		} else {
			let ptr = self.notif_addr as *mut u16;

			unsafe {
				unaligned_volatile_store(
					ptr,
					u16::from_ne_bytes(notif_data[0..2].try_into().unwrap()),
				);
			}
		}

		fence(Ordering::Release);
	}
}

/// Wraps a [IsrStatusRaw](structs.isrstatusraw.html) in order to preserve
/// the original structure and allow interaction with the device via
/// the structure.
///
/// Provides a safe API for Raw structure and allows interaction with the device via
/// the structure.
pub struct IsrStatus {
	raw: &'static mut IsrStatusRaw,
}

impl IsrStatus {
	pub fn new(registers: &mut MmioRegisterLayout) -> Self {
		let ptr = &mut registers.interrupt_status as *mut _;
		let raw: &'static mut IsrStatusRaw = unsafe { &mut *(ptr as *mut IsrStatusRaw) };

		IsrStatus { raw }
	}

	pub fn is_interrupt(&self) -> bool {
		unsafe {
			let status = read_volatile(&self.raw.interrupt_status);
			status & 0x1 == 0x1
		}
	}

	pub fn is_cfg_change(&self) -> bool {
		unsafe {
			let status = read_volatile(&self.raw.interrupt_status);
			status & 0x2 == 0x2
		}
	}

	pub fn acknowledge(&mut self) {
		unsafe {
			let status = read_volatile(&self.raw.interrupt_status);
			write_volatile(&mut self.raw.interrupt_ack, status);
		}
	}
}

#[repr(C)]
struct IsrStatusRaw {
	interrupt_status: u32,
	interrupt_ack: u32,
}

pub(crate) enum VirtioDriver {
	#[cfg(feature = "tcp")]
	Network(VirtioNetDriver),
}

#[allow(unused_variables)]
pub(crate) fn init_device(
	registers: &'static mut MmioRegisterLayout,
	irq_no: u8,
) -> Result<VirtioDriver, DriverError> {
	let dev_id: u16 = 0;

	if registers.version == 0x1 {
		error!("Legacy interface isn't supported!");
		return Err(DriverError::InitVirtioDevFail(
			VirtioError::DevNotSupported(dev_id),
		));
	}

	// Verify the device-ID to find the network card
	match registers.device_id {
		#[cfg(feature = "tcp")]
		DevId::VIRTIO_DEV_ID_NET => {
			match VirtioNetDriver::init(dev_id, registers, irq_no) {
				Ok(virt_net_drv) => {
					info!("Virtio network driver initialized.");
					// Install interrupt handler
					irq_install_handler(irq_no, network_irqhandler);
					add_irq_name(irq_no, "virtio_net");

					Ok(VirtioDriver::Network(virt_net_drv))
				}
				Err(virtio_error) => {
					error!("Virtio network driver could not be initialized with device");
					Err(DriverError::InitVirtioDevFail(virtio_error))
				}
			}
		}
		_ => {
			error!(
				"Device with id {:?} is currently not supported!",
				registers.device_id
			);
			// Return Driver error inidacting device is not supported
			Err(DriverError::InitVirtioDevFail(
				VirtioError::DevNotSupported(dev_id),
			))
		}
	}
}

/// The Layout of MMIO Device
#[repr(C, align(4))]
pub struct MmioRegisterLayout {
	magic_value: u32,
	version: u32,
	device_id: DevId,
	vendor_id: u32,

	device_features: u32,
	device_features_sel: u32,
	_reserved0: [u32; 2],
	driver_features: u32,
	driver_features_sel: u32,

	guest_page_size: u32, // legacy only
	_reserved1: u32,

	queue_sel: u32,
	queue_num_max: u32,
	queue_num: u32,
	queue_align: u32, // legacy only
	queue_pfn: u32,   // legacy only
	queue_ready: u32, // non-legacy only
	_reserved2: [u32; 2],
	queue_notify: u32,
	_reserved3: [u32; 3],

	interrupt_status: u32,
	interrupt_ack: u32,
	_reserved4: [u32; 2],

	status: u32,
	_reserved5: [u32; 3],

	queue_desc_low: u32,  // non-legacy only
	queue_desc_high: u32, // non-legacy only
	_reserved6: [u32; 2],
	queue_driver_low: u32,  // non-legacy only
	queue_driver_high: u32, // non-legacy only
	_reserved7: [u32; 2],
	queue_device_low: u32,  // non-legacy only
	queue_device_high: u32, // non-legacy only
	_reserved8: [u32; 21],

	config_generation: u32, // non-legacy only
	config: [u32; 3],
}

impl MmioRegisterLayout {
	pub fn get_magic_value(&self) -> u32 {
		unsafe { read_volatile(&self.magic_value) }
	}

	pub fn get_version(&self) -> u32 {
		unsafe { read_volatile(&self.version) }
	}

	pub fn get_device_id(&self) -> DevId {
		unsafe { read_volatile(&self.device_id) }
	}

	pub fn enable_queue(&mut self, sel: u32) {
		unsafe {
			write_volatile(&mut self.queue_sel, sel);
			write_volatile(&mut self.queue_ready, 1u32);
		}
	}

	pub fn get_max_queue_size(&mut self, sel: u32) -> u32 {
		unsafe {
			write_volatile(&mut self.queue_sel, sel);
			read_volatile(&self.queue_num_max)
		}
	}

	pub fn set_queue_size(&mut self, sel: u32, size: u32) -> u32 {
		unsafe {
			write_volatile(&mut self.queue_sel, sel);

			let num_max = read_volatile(&self.queue_num_max);

			if num_max >= size {
				write_volatile(&mut self.queue_num, size);
				size
			} else {
				write_volatile(&mut self.queue_num, num_max);
				num_max
			}
		}
	}

	pub fn set_ring_addr(&mut self, sel: u32, addr: PhysAddr) {
		unsafe {
			write_volatile(&mut self.queue_sel, sel);
			write_volatile(&mut self.queue_desc_low, addr.as_u64() as u32);
			write_volatile(&mut self.queue_desc_high, (addr.as_u64() >> 32) as u32);
		}
	}

	pub fn set_drv_ctrl_addr(&mut self, sel: u32, addr: PhysAddr) {
		unsafe {
			write_volatile(&mut self.queue_sel, sel);
			write_volatile(&mut self.queue_driver_low, addr.as_u64() as u32);
			write_volatile(&mut self.queue_driver_high, (addr.as_u64() >> 32) as u32);
		}
	}

	pub fn set_dev_ctrl_addr(&mut self, sel: u32, addr: PhysAddr) {
		unsafe {
			write_volatile(&mut self.queue_sel, sel);
			write_volatile(&mut self.queue_device_low, addr.as_u64() as u32);
			write_volatile(&mut self.queue_device_high, (addr.as_u64() >> 32) as u32);
		}
	}

	pub fn get_queue_ready(&mut self, sel: u32) -> bool {
		unsafe {
			write_volatile(&mut self.queue_sel, sel);
			read_volatile(&self.queue_ready) != 0
		}
	}

	pub fn dev_features(&mut self) -> u64 {
		// Indicate device to show high 32 bits in device_feature field.
		// See Virtio specification v1.1. - 4.1.4.3
		unsafe {
			write_volatile(&mut self.device_features_sel, 1u32);

			// read high 32 bits of device features
			let mut dev_feat = u64::from(read_volatile(&self.device_features)) << 32;

			// Indicate device to show low 32 bits in device_feature field.
			// See Virtio specification v1.1. - 4.1.4.3
			write_volatile(&mut self.device_features_sel, 0u32);

			// read low 32 bits of device features
			dev_feat |= u64::from(read_volatile(&self.device_features));

			dev_feat
		}
	}

	/// Write selected features into driver_select field.
	pub fn set_drv_features(&mut self, feats: u64) {
		let high: u32 = (feats >> 32) as u32;
		let low: u32 = feats as u32;

		unsafe {
			// Indicate to device that driver_features field shows low 32 bits.
			// See Virtio specification v1.1. - 4.1.4.3
			write_volatile(&mut self.driver_features_sel, 0u32);

			// write low 32 bits of device features
			write_volatile(&mut self.driver_features, low);

			// Indicate to device that driver_features field shows high 32 bits.
			// See Virtio specification v1.1. - 4.1.4.3
			write_volatile(&mut self.driver_features_sel, 1u32);

			// write high 32 bits of device features
			write_volatile(&mut self.driver_features, high);
		}
	}

	pub fn get_config(&mut self) -> [u32; 3] {
		// see Virtio specification v1.1 -  2.4.1
		unsafe {
			loop {
				let before = read_volatile(&self.config_generation);
				fence(Ordering::SeqCst);
				let config = read_volatile(&self.config);
				fence(Ordering::SeqCst);
				let after = read_volatile(&self.config_generation);
				fence(Ordering::SeqCst);

				if before == after {
					return config;
				}
			}
		}
	}

	pub fn print_information(&mut self) {
		infoheader!(" MMIO RREGISTER LAYOUT INFORMATION ");

		infoentry!("Device version", "{:#X}", self.get_version());
		infoentry!("Device ID", "{:?}", unsafe {
			read_volatile(&self.device_id)
		});
		infoentry!("Vendor ID", "{:#X}", unsafe {
			read_volatile(&self.vendor_id)
		});
		infoentry!("Device Features", "{:#X}", self.dev_features());
		infoentry!("Interrupt status", "{:#X}", unsafe {
			read_volatile(&self.interrupt_status)
		});
		infoentry!("Device status", "{:#X}", unsafe {
			read_volatile(&self.status)
		});
		infoentry!("Configuration space", "{:#X?}", self.get_config());

		infofooter!();
	}
}