hvlite_core/emuplat/
watchdog.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use vmcore::non_volatile_store::NonVolatileStore;
5use watchdog_core::platform::WatchdogPlatform;
6use watchdog_vmgs_format::WatchdogVmgsFormatStore;
7use watchdog_vmgs_format::WatchdogVmgsFormatStoreError;
8
9/// An implementation of [`WatchdogPlatform`] for use with both the UEFI
10/// watchdog and the Guest Watchdog in HvLite.
11pub struct HvLiteWatchdogPlatform {
12    store: WatchdogVmgsFormatStore,
13    on_timeout: Box<dyn Fn() + Send + Sync>,
14}
15
16impl HvLiteWatchdogPlatform {
17    pub async fn new(
18        store: Box<dyn NonVolatileStore>,
19        on_timeout: Box<dyn Fn() + Send + Sync>,
20    ) -> Result<Self, WatchdogVmgsFormatStoreError> {
21        Ok(HvLiteWatchdogPlatform {
22            store: WatchdogVmgsFormatStore::new(store).await?,
23            on_timeout,
24        })
25    }
26}
27
28#[async_trait::async_trait]
29impl WatchdogPlatform for HvLiteWatchdogPlatform {
30    async fn on_timeout(&mut self) {
31        let res = self.store.set_boot_failure().await;
32        if let Err(e) = res {
33            tracing::error!(
34                error = &e as &dyn std::error::Error,
35                "error persisting watchdog status"
36            );
37        }
38
39        (self.on_timeout)()
40    }
41
42    async fn read_and_clear_boot_status(&mut self) -> bool {
43        let res = self.store.read_and_clear_boot_status().await;
44        match res {
45            Ok(status) => status,
46            Err(e) => {
47                tracing::error!(
48                    error = &e as &dyn std::error::Error,
49                    "error reading watchdog status"
50                );
51                // assume no failure
52                false
53            }
54        }
55    }
56}