Serenity Operating System
at hosted 238 lines 7.4 kB view raw
1/* 2 * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/Assertions.h> 28#include <AK/String.h> 29#include <Kernel/Arch/i386/CPU.h> 30#include <Kernel/CommandLine.h> 31#include <Kernel/Devices/VMWareBackdoor.h> 32#include <Kernel/MousePacket.h> 33#include <LibBareMetal/IO.h> 34 35namespace Kernel { 36 37#define VMWARE_CMD_GETVERSION 0x0a 38 39#define VMMOUSE_READ_ID 0x45414552 40#define VMMOUSE_DISABLE 0x000000f5 41#define VMMOUSE_REQUEST_RELATIVE 0x4c455252 42#define VMMOUSE_REQUEST_ABSOLUTE 0x53424152 43 44#define VMMOUSE_QEMU_VERSION 0x3442554a 45#define VMMOUSE_LEFT_CLICK 0x20 46#define VMMOUSE_RIGHT_CLICK 0x10 47#define VMMOUSE_MIDDLE_CLICK 0x08 48 49#define VMWARE_MAGIC 0x564D5868 50#define VMWARE_PORT 0x5658 51#define VMWARE_PORT_HIGHBANDWIDTH 0x5659 52 53//#define VMWAREBACKDOOR_DEBUG 54 55inline void vmware_out(VMWareCommand& command) 56{ 57 command.magic = VMWARE_MAGIC; 58 command.port = VMWARE_PORT; 59 command.si = 0; 60 command.di = 0; 61 asm volatile("in %%dx, %0" 62 : "+a"(command.ax), "+b"(command.bx), "+c"(command.cx), "+d"(command.dx), "+S"(command.si), "+D"(command.di)); 63} 64 65inline void vmware_high_bandwidth_send(VMWareCommand& command) 66{ 67 68 command.magic = VMWARE_MAGIC; 69 command.port = VMWARE_PORT_HIGHBANDWIDTH; 70 71 asm volatile("cld; rep; outsb" 72 : "+a"(command.ax), "+b"(command.bx), "+c"(command.cx), "+d"(command.dx), "+S"(command.si), "+D"(command.di)); 73} 74 75inline void vmware_high_bandwidth_get(VMWareCommand& command) 76{ 77 command.magic = VMWARE_MAGIC; 78 command.port = VMWARE_PORT_HIGHBANDWIDTH; 79 asm volatile("cld; rep; insb" 80 : "+a"(command.ax), "+b"(command.bx), "+c"(command.cx), "+d"(command.dx), "+S"(command.si), "+D"(command.di)); 81} 82 83static VMWareBackdoor* s_vmware_backdoor; 84 85static bool detect_presence() 86{ 87 VMWareCommand command; 88 command.bx = ~VMWARE_MAGIC; 89 command.command = VMWARE_CMD_GETVERSION; 90 vmware_out(command); 91 if (command.bx != VMWARE_MAGIC || command.ax == 0xFFFFFFFF) 92 return false; 93 return true; 94} 95 96VMWareBackdoor* VMWareBackdoor::initialize() 97{ 98 if (!detect_presence()) 99 return nullptr; 100 101 s_vmware_backdoor = new VMWareBackdoor; 102 klog() << "VMWare backdoor opened."; 103 return s_vmware_backdoor; 104} 105 106VMWareBackdoor* VMWareBackdoor::the() 107{ 108 return s_vmware_backdoor; 109} 110 111VMWareBackdoor::VMWareBackdoor() 112{ 113 if (kernel_command_line().lookup("vmmouse").value_or("on") == "on") 114 enable_absolute_vmmouse(); 115} 116 117bool VMWareBackdoor::detect_vmmouse() 118{ 119 VMWareCommand command; 120 command.bx = VMMOUSE_READ_ID; 121 command.command = VMMOUSE_COMMAND; 122 send(command); 123 command.bx = 1; 124 command.command = VMMOUSE_DATA; 125 send(command); 126 if (command.ax != VMMOUSE_QEMU_VERSION) 127 return false; 128 return true; 129} 130bool VMWareBackdoor::vmmouse_is_absolute() const 131{ 132 return m_vmmouse_absolute; 133} 134 135void VMWareBackdoor::enable_absolute_vmmouse() 136{ 137 InterruptDisabler disabler; 138 if (!detect_vmmouse()) 139 return; 140 klog() << "VMWareBackdoor: Enabling absolute mouse mode"; 141 142 VMWareCommand command; 143 144 command.bx = 0; 145 command.command = VMMOUSE_STATUS; 146 send(command); 147 if (command.ax == 0xFFFF0000) { 148 klog() << "VMWareBackdoor: VMMOUSE_STATUS got bad status"; 149 return; 150 } 151 152 // Enable absolute vmmouse 153 command.bx = VMMOUSE_REQUEST_ABSOLUTE; 154 command.command = VMMOUSE_COMMAND; 155 send(command); 156 m_vmmouse_absolute = true; 157} 158void VMWareBackdoor::disable_absolute_vmmouse() 159{ 160 InterruptDisabler disabler; 161 VMWareCommand command; 162 command.bx = VMMOUSE_REQUEST_RELATIVE; 163 command.command = VMMOUSE_COMMAND; 164 send(command); 165 m_vmmouse_absolute = false; 166} 167 168void VMWareBackdoor::send_high_bandwidth(VMWareCommand& command) 169{ 170 vmware_high_bandwidth_send(command); 171#ifdef VMWAREBACKDOOR_DEBUG 172 dbg() << "VMWareBackdoor Command High bandwidth Send Results: EAX " << String::format("%x", command.ax) << " EBX " << String::format("%x", command.bx) << " ECX " << String::format("%x", command.cx) << " EDX " << String::format("%x", command.dx); 173#endif 174} 175 176void VMWareBackdoor::get_high_bandwidth(VMWareCommand& command) 177{ 178 vmware_high_bandwidth_get(command); 179#ifdef VMWAREBACKDOOR_DEBUG 180 dbg() << "VMWareBackdoor Command High bandwidth Get Results: EAX " << String::format("%x", command.ax) << " EBX " << String::format("%x", command.bx) << " ECX " << String::format("%x", command.cx) << " EDX " << String::format("%x", command.dx); 181#endif 182} 183 184void VMWareBackdoor::send(VMWareCommand& command) 185{ 186 vmware_out(command); 187#ifdef VMWAREBACKDOOR_DEBUG 188 dbg() << "VMWareBackdoor Command Send Results: EAX " << String::format("%x", command.ax) << " EBX " << String::format("%x", command.bx) << " ECX " << String::format("%x", command.cx) << " EDX " << String::format("%x", command.dx); 189#endif 190} 191 192Optional<MousePacket> VMWareBackdoor::receive_mouse_packet() 193{ 194 VMWareCommand command; 195 command.bx = 0; 196 command.command = VMMOUSE_STATUS; 197 send(command); 198 if (command.ax == 0xFFFF0000) { 199#ifdef PS2MOUSE_DEBUG 200 klog() << "PS2MouseDevice: Resetting VMWare mouse"; 201#endif 202 disable_absolute_vmmouse(); 203 enable_absolute_vmmouse(); 204 return {}; 205 } 206 int words = command.ax & 0xFFFF; 207 208 if (!words || words % 4) 209 return {}; 210 command.size = 4; 211 command.command = VMMOUSE_DATA; 212 send(command); 213 214 int buttons = (command.ax & 0xFFFF); 215 int x = (command.bx); 216 int y = (command.cx); 217 int z = (command.dx); 218 219#ifdef PS2MOUSE_DEBUG 220 dbg() << "Absolute Mouse: Buttons " << String::format("%x", buttons); 221 dbg() << "Mouse: X " << x << ", Y " << y << ", Z " << z; 222#endif 223 MousePacket packet; 224 packet.x = x; 225 packet.y = y; 226 packet.z = z; 227 if (buttons & VMMOUSE_LEFT_CLICK) 228 packet.buttons |= MousePacket::LeftButton; 229 if (buttons & VMMOUSE_RIGHT_CLICK) 230 packet.buttons |= MousePacket::RightButton; 231 if (buttons & VMMOUSE_MIDDLE_CLICK) 232 packet.buttons |= MousePacket::MiddleButton; 233 234 packet.is_relative = false; 235 return packet; 236} 237 238}