Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert api.c into Rust code. #9

Merged
merged 13 commits into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions hfo2/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions hfo2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ bitflags = "1.0"
static_assertions = "0.3.2"
reduce = "0.1"
arrayvec = { version = "0.4", default-features = false }
memoffset = "0.5.1"
137 changes: 137 additions & 0 deletions hfo2/src/abi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 파일은 원래 어디있던 건가요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inc/vmapi/hf/abi.h 에 있었습니다.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue로 남깁시다: filename convention

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue를 만들었습니다: #15

* Copyright 2019 Sanguk Park
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

use crate::types::*;

#[repr(C)]
#[derive(Copy, Clone)]
pub enum HfVCpuRunCode {
/// The vCPU has been preempted but still has work to do. If the scheduling
/// quantum has not expired, the scheduler MUST call `hf_vcpu_run` on the
/// vCPU to allow it to continue.
Preempted = 0,

/// The vCPU has voluntarily yielded the CPU. The scheduler SHOULD take a
/// scheduling decision to give cycles to those that need them but MUST
/// call `hf_vcpu_run` on the vCPU at a later point.
Yield = 1,

/// The vCPU is blocked waiting for an interrupt. The scheduler MUST take
/// it off the run queue and not call `hf_vcpu_run` on the vCPU until it
/// has injected an interrupt, received `HF_VCPU_RUN_WAKE_UP` for it from
/// another vCPU or the timeout provided in `hf_vcpu_run_return.sleep` is
/// not `HF_SLEEP_INDEFINITE` and the specified duration has expired.
WaitForInterrupt = 2,

/// The vCPU is blocked waiting for a message. The scheduler MUST take it
/// off the run queue and not call `hf_vcpu_run` on the vCPU until it has
/// injected an interrupt, sent it a message, or received
/// `HF_VCPU_RUN_WAKE_UP` for it from another vCPU from another vCPU or
/// the timeout provided in `hf_vcpu_run_return.sleep` is not
/// `HF_SLEEP_INDEFINITE` and the specified duration has expired.
WaitForMessage = 3,

/// Hafnium would like `hf_vcpu_run` to be called on another vCPU,
/// specified by `hf_vcpu_run_return.wake_up`. The scheduler MUST either
/// wake the vCPU in question up if it is blocked, or preempt and re-run
/// it if it is already running somewhere. This gives Hafnium a chance to
/// update any CPU state which might have changed.
WakeUp = 4,

/// A message has been sent by the vCPU. The scheduler MUST run a vCPU from
/// the recipient VM and priority SHOULD be given to those vCPUs that are
/// waiting for a message.
Message = 5,

/// The vCPU has made the mailbox writable and there are pending waiters.
/// The scheduler MUST call hf_mailbox_waiter_get() repeatedly and notify
/// all waiters by injecting an HF_MAILBOX_WRITABLE_INTID interrupt.
NotifyWaiters = 6,

/// The vCPU has aborted triggering the whole VM to abort. The scheduler
/// MUST treat this as `HF_VCPU_RUN_WAIT_FOR_INTERRUPT` for this vCPU and
/// `HF_VCPU_RUN_WAKE_UP` for all the other vCPUs of the VM.
Aborted = 7,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct HfVCpuRunWakeUp {
pub vm_id: spci_vm_id_t,
pub vcpu: spci_vcpu_index_t,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct HfVCpuRunMessage {
pub vm_id: spci_vm_id_t,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct HfVCpuRunSleep {
pub ns: u64,
}

#[repr(C)]
pub union HfVCpuRunDetail {
pub wake_up: HfVCpuRunWakeUp,
pub message: HfVCpuRunMessage,
pub sleep: HfVCpuRunSleep,
}

#[repr(C)]
pub struct HfVCpuRunReturn {
pub code: HfVCpuRunCode,
pub detail: HfVCpuRunDetail,
}

#[repr(C)]
#[derive(PartialEq)]
pub enum HfShare {
/// Relinquish ownership and access to the memory and pass them to the
/// recipient.
Give,

/// Retain ownership of the memory but relinquish access to the recipient.
Lend,

/// Retain ownership and access but additionally allow access to the
/// recipient.
Share,
}

/// Encode an HfVCpuRunReturn struct in the 64-bit packing ABI.
#[inline]
pub unsafe fn hf_vcpu_run_return_encode(res: HfVCpuRunReturn) -> u64 {
let mut ret = res.code as u64 & 0xff;

match res.code {
HfVCpuRunCode::WakeUp => {
ret |= (res.detail.wake_up.vm_id as u64) << 32;
ret |= (res.detail.wake_up.vcpu as u64) << 16;
}
HfVCpuRunCode::Message => {
ret |= (res.detail.message.vm_id as u64) << 8;
}
HfVCpuRunCode::WaitForInterrupt | HfVCpuRunCode::WaitForMessage => {
ret |= (res.detail.sleep.ns as u64) << 8;
}
_ => (),
}

ret
}
Loading