Skip to content

Commit

Permalink
Removes vmm_start (#1125)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon authored Nov 20, 2024
1 parent 12c65cd commit 037f9cb
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 242 deletions.
81 changes: 0 additions & 81 deletions gui/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,6 @@ enum DisplayResolution {
DisplayResolution_UltraHd,
};

/**
* Log category.
*
* The reason we need this because cbindgen is not good at exporting dependency types so we can't
* use [`ConsoleType`] directly. See https://github.com/mozilla/cbindgen/issues/667 for an example
* of the problem.
*/
enum VmmLog {
VmmLog_Info,
VmmLog_Warn,
VmmLog_Error,
};

/**
* Encapsulate a debugger connection.
*/
Expand Down Expand Up @@ -74,66 +61,6 @@ struct RustError;
*/
struct Vmm;

/**
* Contains objects required to render the screen.
*/
struct VmmScreen {
#if !defined(__APPLE__)
size_t vk_instance
#endif
;
#if !defined(__APPLE__)
size_t vk_device
#endif
;
#if !defined(__APPLE__)
size_t vk_surface
#endif
;
#if defined(__APPLE__)
size_t view
#endif
;
};

/**
* Contains VMM event information.
*/
enum VmmEvent_Tag {
VmmEvent_Error,
VmmEvent_Exiting,
VmmEvent_Log,
VmmEvent_Breakpoint,
};

struct VmmEvent_Error_Body {
const struct RustError *reason;
};

struct VmmEvent_Exiting_Body {
bool success;
};

struct VmmEvent_Log_Body {
enum VmmLog ty;
const char *data;
size_t len;
};

struct VmmEvent_Breakpoint_Body {
struct KernelStop *stop;
};

struct VmmEvent {
enum VmmEvent_Tag tag;
union {
struct VmmEvent_Error_Body error;
struct VmmEvent_Exiting_Body exiting;
struct VmmEvent_Log_Body log;
struct VmmEvent_Breakpoint_Body breakpoint;
};
};

/**
* Result of [`vmm_dispatch_debug()`].
*/
Expand Down Expand Up @@ -228,14 +155,6 @@ struct RustError *update_firmware(const char *root,
void *cx,
void (*status)(const char*, uint64_t, uint64_t, void*));

struct Vmm *vmm_start(const char *kernel,
const struct VmmScreen *screen,
const struct Profile *profile,
struct DebugClient *debugger,
void (*event_handler)(const struct VmmEvent*, void*),
void *cx,
struct RustError **err);

void vmm_free(struct Vmm *vmm);

struct RustError *vmm_draw(struct Vmm *vmm);
Expand Down
104 changes: 0 additions & 104 deletions gui/main_window.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "main_window.hpp"
#include "app_data.hpp"
#include "display_settings.hpp"
#include "game_models.hpp"
#include "launch_settings.hpp"
#include "path.hpp"
Expand Down Expand Up @@ -32,13 +31,11 @@
#include <QUrl>

#include <filesystem>
#include <iostream>
#include <utility>

#ifndef _WIN32
#include <fcntl.h>
#endif
#include <string.h>

namespace Args {
const QCommandLineOption debug("debug", "Immediate launch the VMM in debug mode.", "addr", "127.0.0.1:1234");
Expand Down Expand Up @@ -419,19 +416,6 @@ void MainWindow::waitKernelExit(bool success)
m_main->setCurrentIndex(0);
}

void MainWindow::log(VmmLog type, const QString &msg)
{
switch (type) {
case VmmLog_Info:
std::cout << msg.toStdString();
break;
case VmmLog_Warn:
case VmmLog_Error:
std::cerr << msg.toStdString();
break;
}
}

void MainWindow::setupDebugger()
{
// Enable non-blocking on debug socket. On Windows QSocketNotifier will do this for us.
Expand Down Expand Up @@ -635,49 +619,6 @@ void MainWindow::startVmm(Rust<DebugClient> &&debug)

// Swap launch settings with the screen before getting a Vulkan surface otherwise it will fail.
m_main->setCurrentIndex(1);

// Setup the screen.
VmmScreen screen;
Rust<RustError> error;
Rust<Vmm> vmm;

memset(&screen, 0, sizeof(screen));

#ifdef __APPLE__
screen.view = m_screen->winId();
#else
screen.vk_instance = reinterpret_cast<size_t>(m_screen->vulkanInstance()->vkInstance());
screen.vk_device = reinterpret_cast<size_t>(m_launch->currentDisplayDevice()->handle());
screen.vk_surface = reinterpret_cast<size_t>(QVulkanInstance::surfaceForWindow(m_screen));

if (!screen.vk_surface) {
QMessageBox::critical(this, "Error", "Couldn't create VkSurfaceKHR.");
m_main->setCurrentIndex(0);
return;
}
#endif

// Run.
vmm = vmm_start(
kernel.c_str(),
&screen,
m_launch->currentProfile(),
debug.release(),
MainWindow::vmmHandler,
this,
&error);

if (!vmm) {
QMessageBox::critical(
this,
"Error",
QString("Couldn't run %1: %2").arg(kernel.c_str()).arg(error_message(error)));
m_main->setCurrentIndex(0);
return;
}

m_vmm = std::move(vmm);
m_screen->requestUpdate();
}

bool MainWindow::requireVmmStopped()
Expand Down Expand Up @@ -726,48 +667,3 @@ void MainWindow::killVmm()
delete m_debugNoti;
m_debugNoti = nullptr;
}

void MainWindow::vmmHandler(const VmmEvent *ev, void *cx)
{
// This method will be called from non-main thread.
auto w = reinterpret_cast<MainWindow *>(cx);

switch (ev->tag) {
case VmmEvent_Error:
QMetaObject::invokeMethod(
w,
&MainWindow::vmmError,
Qt::QueuedConnection,
QString(error_message(ev->error.reason)));
break;
case VmmEvent_Exiting:
QMetaObject::invokeMethod(
w,
&MainWindow::waitKernelExit,
Qt::QueuedConnection,
ev->exiting.success);
break;
case VmmEvent_Log:
QMetaObject::invokeMethod(
w,
&MainWindow::log,
Qt::QueuedConnection,
ev->log.ty,
QString::fromUtf8(ev->log.data, ev->log.len));
break;
case VmmEvent_Breakpoint:
if (auto stop = ev->breakpoint.stop; stop) {
QMetaObject::invokeMethod(
w,
&MainWindow::dispatchDebug,
Qt::QueuedConnection,
stop);
} else {
QMetaObject::invokeMethod(
w,
&MainWindow::setupDebugger,
Qt::QueuedConnection);
}
break;
}
}
3 changes: 0 additions & 3 deletions gui/main_window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,13 @@ private slots:
void debuggerConnected();
void vmmError(const QString &msg);
void waitKernelExit(bool success);
void log(VmmLog type, const QString &msg);
void setupDebugger();
void dispatchDebug(KernelStop *stop);
bool loadGame(const QString &gameId);
bool requireVmmStopped();
void stopDebug();
void killVmm();

static void vmmHandler(const VmmEvent *ev, void *cx);

const QCommandLineParser &m_args;
QStackedWidget *m_main;
ProfileList *m_profiles;
Expand Down
54 changes: 0 additions & 54 deletions gui/src/vmm/ffi.rs
Original file line number Diff line number Diff line change
@@ -1,64 +1,10 @@
use super::{DebugResult, DispatchDebugResult, KernelStop, Vmm, VmmEvent, VmmScreen};
use crate::debug::DebugClient;
use crate::error::RustError;
use crate::profile::Profile;
use crate::screen::Screen;
use gdbstub::stub::state_machine::GdbStubStateMachine;
use std::ffi::{c_char, c_void, CStr};
use std::ptr::null_mut;
use std::sync::atomic::Ordering;

#[no_mangle]
pub unsafe extern "C" fn vmm_start(
kernel: *const c_char,
screen: *const VmmScreen,
profile: *const Profile,
debugger: *mut DebugClient,
event_handler: unsafe extern "C" fn(*const VmmEvent, *mut c_void),
cx: *mut c_void,
err: *mut *mut RustError,
) -> *mut Vmm {
// Consume the debugger now to prevent memory leak in case of error.
let debugger = if debugger.is_null() {
None
} else {
Some(*Box::from_raw(debugger))
};

// Check if path UTF-8.
let path = match CStr::from_ptr(kernel).to_str() {
Ok(v) => v,
Err(_) => {
*err = RustError::new("path of the kernel is not UTF-8").into_c();
return null_mut();
}
};

let profile = unsafe { &*profile };
let screen = unsafe { &*screen };

let screen = match crate::screen::Default::from_screen(screen) {
Ok(v) => v,
Err(e) => {
*err = RustError::with_source("couldn't setup a screen", e).into_c();
return null_mut();
}
};

// Cast to make the closure Send + Sync.
let cx_usize = cx as usize;

match Vmm::new(path, screen, profile, debugger, move |event| {
event_handler(&event, cx_usize as *mut c_void)
}) {
Ok(vmm) => Box::into_raw(Box::new(vmm)),
Err(e) => {
*err = RustError::wrap(e).into_c();
null_mut()
}
}
}

#[no_mangle]
pub unsafe extern "C" fn vmm_free(vmm: *mut Vmm) {
drop(Box::from_raw(vmm));
Expand Down

0 comments on commit 037f9cb

Please sign in to comment.