ccp_msr/linux_x86_64/
errors.rs

1/*
2 * Copyright 2024 Fluence DAO
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
17use nix::errno::Errno;
18use thiserror::Error as ThisError;
19
20use ccp_shared::types::LogicalCoreId;
21
22#[derive(ThisError, Debug)]
23pub enum MSRError {
24    #[error("opening MSR file '/dev/cpu/{core_id:}/msr' for read there is an error: {io_error:?}")]
25    OpenForRead {
26        core_id: LogicalCoreId,
27        io_error: std::io::Error,
28    },
29
30    #[error(
31        "opening MSR file '/dev/cpu/{core_id:}/msr' for write there is an error: {io_error:?}"
32    )]
33    OpenForWrite {
34        core_id: LogicalCoreId,
35        io_error: std::io::Error,
36    },
37
38    #[error(
39        "reading from register_id {register_id:} and MSR file '/dev/cpu/{core_id:}/msr' there is an error: {errno:?}"
40    )]
41    ReadWNoErr {
42        register_id: u32,
43        core_id: LogicalCoreId,
44        errno: Errno,
45    },
46
47    #[error(
48            "writing value {value:} for register_id {register_id:} into MSR file '/dev/cpu/{core_id:}/msr' there is an error: {errno:?}"j
49        )]
50    WriteWNoErr {
51        value: u64,
52        register_id: u32,
53        core_id: LogicalCoreId,
54        errno: Errno,
55    },
56}
57
58impl MSRError {
59    pub(crate) fn open_for_read(core_id: LogicalCoreId, io_error: std::io::Error) -> Self {
60        Self::OpenForRead { core_id, io_error }
61    }
62
63    pub(crate) fn open_for_write(core_id: LogicalCoreId, io_error: std::io::Error) -> Self {
64        Self::OpenForWrite { core_id, io_error }
65    }
66
67    pub(crate) fn read_w_no_err(register_id: u32, core_id: LogicalCoreId, errno: Errno) -> Self {
68        Self::ReadWNoErr {
69            register_id,
70            core_id,
71            errno,
72        }
73    }
74
75    pub(crate) fn write_w_no_err(
76        value: u64,
77        register_id: u32,
78        core_id: LogicalCoreId,
79        errno: Errno,
80    ) -> Self {
81        Self::WriteWNoErr {
82            value,
83            register_id,
84            core_id,
85            errno,
86        }
87    }
88}