1 | /* $Id: apm.c 84752 2020-06-10 10:58:33Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * APM BIOS support. Implements APM version 1.2.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2004-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include <stdint.h>
|
---|
19 | #include <string.h>
|
---|
20 | #include "biosint.h"
|
---|
21 | #include "inlines.h"
|
---|
22 | #include "VBox/bios.h"
|
---|
23 |
|
---|
24 | #if DEBUG_APM
|
---|
25 | # define BX_DEBUG_APM(...) BX_DEBUG(__VA_ARGS__)
|
---|
26 | #else
|
---|
27 | # define BX_DEBUG_APM(...)
|
---|
28 | #endif
|
---|
29 |
|
---|
30 | /* Implemented in assembly. */
|
---|
31 | extern void apm_pm16_entry(void);
|
---|
32 | #pragma aux apm_pm16_entry "*"
|
---|
33 |
|
---|
34 | #if VBOX_BIOS_CPU >= 80386
|
---|
35 | extern void apm_pm32_entry(void);
|
---|
36 | #pragma aux apm_pm32_entry "*"
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | /* APM function codes. */
|
---|
40 | enum apm_func {
|
---|
41 | APM_CHECK = 0x00, /* APM Installation Check */
|
---|
42 | APM_RM_CONN = 0x01, /* APM Real Mode Interface Connect */
|
---|
43 | APM_PM_CONN = 0x02, /* APM Protected Mode 16-bit Interface Connect */
|
---|
44 | APM_32_CONN = 0x03, /* APM Protected Mode 32-bit Interface Connect */
|
---|
45 | APM_DISCONN = 0x04, /* APM Interface Disconnect */
|
---|
46 | APM_IDLE = 0x05, /* CPU Idle */
|
---|
47 | APM_BUSY = 0x06, /* CPU Busy */
|
---|
48 | APM_SET_PWR = 0x07, /* Set Power State */
|
---|
49 | APM_ENBL_PM = 0x08, /* Enable/Disable Power Management */
|
---|
50 | APM_SET_DFL = 0x09, /* Restore APM BIOS Power-On Defaults */
|
---|
51 | APM_STATUS = 0x0A, /* Get Power Status */
|
---|
52 | APM_GET_EVT = 0x0B, /* Get PM Event */
|
---|
53 | APM_GET_PWR = 0x0C, /* Get Power State */
|
---|
54 | APM_DEVPM = 0x0D, /* Enable/Disable Device Power Management */
|
---|
55 | APM_DRV_VER = 0x0E, /* APM Driver Version */
|
---|
56 | APM_ENGAGE = 0x0F, /* Engage/Disengage Power Management */
|
---|
57 | APM_GET_CAP = 0x10 /* Get Capabilities */
|
---|
58 | };
|
---|
59 |
|
---|
60 | enum apm_error {
|
---|
61 | APM_ERR_PM_DISABLED = 0x01, /* Power Management functionality disabled */
|
---|
62 | APM_ERR_RM_INUSE = 0x02, /* Real mode interface connection already established */
|
---|
63 | APM_ERR_NOT_CONN = 0x03, /* Interface not connected */
|
---|
64 | APM_ERR_PM_16_INUSE = 0x05, /* 16-bit protected mode interface connection already established */
|
---|
65 | APM_ERR_NO_PM_16 = 0x06, /* 16-bit protected mode interface not supported */
|
---|
66 | APM_ERR_PM_32_INUSE = 0x07, /* 32-bit protected mode interface connection already established */
|
---|
67 | APM_ERR_NO_PM_32 = 0x08, /* 32-bit protected mode interface not supported */
|
---|
68 | APM_ERR_BAD_DEV_ID = 0x09, /* Unrecognized device ID */
|
---|
69 | APM_ERR_INVAL_PARAM = 0x0A, /* Parameter out of range */
|
---|
70 | APM_ERR_NOT_ENGAGED = 0x0B, /* Interface not engaged */
|
---|
71 | APM_ERR_UNSUPPORTED = 0x0C, /* Function not supported */
|
---|
72 | APM_ERR_NO_RSM_TMR = 0x0D, /* Resume timer disabled */
|
---|
73 | APM_ERR_NO_EVENTS = 0x80 /* No power management events pending */
|
---|
74 | };
|
---|
75 |
|
---|
76 | enum apm_power_state {
|
---|
77 | APM_PS_ENABLED = 0x00, /* APM enabled */
|
---|
78 | APM_PS_STANDBY = 0x01, /* Standby */
|
---|
79 | APM_PS_SUSPEND = 0x02, /* Suspend */
|
---|
80 | APM_PS_OFF = 0x03, /* Suspend */
|
---|
81 | };
|
---|
82 |
|
---|
83 | /// @todo merge with system.c
|
---|
84 | #define AX r.gr.u.r16.ax
|
---|
85 | #define BX r.gr.u.r16.bx
|
---|
86 | #define CX r.gr.u.r16.cx
|
---|
87 | #define DX r.gr.u.r16.dx
|
---|
88 | #define SI r.gr.u.r16.si
|
---|
89 | #define DI r.gr.u.r16.di
|
---|
90 | #define BP r.gr.u.r16.bp
|
---|
91 | #define SP r.gr.u.r16.sp
|
---|
92 | #define FLAGS r.fl.u.r16.flags
|
---|
93 | #define EAX r.gr.u.r32.eax
|
---|
94 | #define EBX r.gr.u.r32.ebx
|
---|
95 | #define ECX r.gr.u.r32.ecx
|
---|
96 | #define EDX r.gr.u.r32.edx
|
---|
97 | #define ES r.es
|
---|
98 |
|
---|
99 | #define APM_BIOS_SEG 0xF000 /* Real-mode APM segment. */
|
---|
100 | #define APM_BIOS_SEG_LEN 0xFFF0 /* Length of APM segment. */
|
---|
101 |
|
---|
102 | /* The APM BIOS interface uses 32-bit registers *only* in the 32-bit
|
---|
103 | * protected mode connect call. Rather than saving/restoring 32-bit
|
---|
104 | * registers all the time, simply set the high words of those registers
|
---|
105 | * when necessary.
|
---|
106 | */
|
---|
107 | void set_ebx_hi(uint16_t val);
|
---|
108 | #pragma aux set_ebx_hi = \
|
---|
109 | ".386" \
|
---|
110 | "shl ebx, 16" \
|
---|
111 | parm [bx] modify exact [bx] nomemory;
|
---|
112 |
|
---|
113 | void set_esi_hi(uint16_t val);
|
---|
114 | #pragma aux set_esi_hi = \
|
---|
115 | ".386" \
|
---|
116 | "shl esi, 16" \
|
---|
117 | parm [si] modify exact [si] nomemory;
|
---|
118 |
|
---|
119 |
|
---|
120 | /* The APM handler has unique requirements. It must be callable from real and
|
---|
121 | * protected mode, both 16-bit and 32-bit. In protected mode, the caller must
|
---|
122 | * ensure that appropriate selectors are available; these only cover the BIOS
|
---|
123 | * code and data, hence the BIOS Data Area or EBDA cannot be accessed. CMOS is
|
---|
124 | * a good place to store information which needs to be accessible from several
|
---|
125 | * different contexts.
|
---|
126 | *
|
---|
127 | * Note that the 32-bit protected-mode handler only needs to thunk down to the
|
---|
128 | * 16-bit code. There's no need for separate 16-bit and 32-bit implementation.
|
---|
129 | */
|
---|
130 |
|
---|
131 | /* Wrapper to avoid unnecessary inlining. */
|
---|
132 | void apm_out_str(const char *s)
|
---|
133 | {
|
---|
134 | if (*s)
|
---|
135 | out_ctrl_str_asm(VBOX_BIOS_SHUTDOWN_PORT, s);
|
---|
136 | }
|
---|
137 |
|
---|
138 | void BIOSCALL apm_function(sys_regs_t r)
|
---|
139 | {
|
---|
140 | BX_DEBUG_APM("APM: AX=%04X BX=%04X CX=%04X\n", AX, BX, CX);
|
---|
141 |
|
---|
142 | CLEAR_CF(); /* Boldly expect success. */
|
---|
143 | switch (GET_AL()) {
|
---|
144 | case APM_CHECK:
|
---|
145 | AX = 0x0102; /* Version 1.2 */
|
---|
146 | BX = 0x504D; /* 'PM' */
|
---|
147 | CX = 3; /* Bits 0/1: 16-bit/32-bit PM interface */
|
---|
148 | break;
|
---|
149 | case APM_RM_CONN:
|
---|
150 | /// @todo validate device ID
|
---|
151 | /// @todo validate current connection state
|
---|
152 | /// @todo change connection state
|
---|
153 | break;
|
---|
154 | case APM_PM_CONN:
|
---|
155 | /// @todo validate device ID
|
---|
156 | /// @todo validate current connection state
|
---|
157 | /// @todo change connection state
|
---|
158 | AX = APM_BIOS_SEG; /* 16-bit PM code segment (RM segment base). */
|
---|
159 | BX = (uint16_t)apm_pm16_entry; /* 16-bit PM entry point offset. */
|
---|
160 | CX = APM_BIOS_SEG; /* 16-bit data segment. */
|
---|
161 | SI = APM_BIOS_SEG_LEN; /* 16-bit PM code segment length. */
|
---|
162 | DI = APM_BIOS_SEG_LEN; /* Data segment length. */
|
---|
163 | break;
|
---|
164 | #if VBOX_BIOS_CPU >= 80386
|
---|
165 | case APM_32_CONN:
|
---|
166 | /// @todo validate device ID
|
---|
167 | /// @todo validate current connection state
|
---|
168 | /// @todo change connection state
|
---|
169 | AX = APM_BIOS_SEG; /* 32-bit PM code segment (RM segment base). */
|
---|
170 | BX = (uint16_t)apm_pm32_entry; /* 32-bit entry point offset. */
|
---|
171 | CX = APM_BIOS_SEG; /* 16-bit code segment. */
|
---|
172 | DX = APM_BIOS_SEG; /* 16-bit data segment. */
|
---|
173 | SI = APM_BIOS_SEG_LEN; /* 32-bit code segment length. */
|
---|
174 | DI = APM_BIOS_SEG_LEN; /* Data segment length. */
|
---|
175 | set_ebx_hi(0);
|
---|
176 | set_esi_hi(APM_BIOS_SEG_LEN); /* 16-bit code segment length. */
|
---|
177 | break;
|
---|
178 | #endif
|
---|
179 | case APM_IDLE:
|
---|
180 | int_enable(); /* Simply halt the CPU with interrupts enabled. */
|
---|
181 | halt();
|
---|
182 | break;
|
---|
183 | case APM_SET_PWR:
|
---|
184 | /// @todo validate device ID
|
---|
185 | /// @todo validate current connection state
|
---|
186 | switch (CX) {
|
---|
187 | case APM_PS_STANDBY:
|
---|
188 | apm_out_str("Standby");
|
---|
189 | break;
|
---|
190 | case APM_PS_SUSPEND:
|
---|
191 | apm_out_str("Suspend");
|
---|
192 | break;
|
---|
193 | case APM_PS_OFF:
|
---|
194 | apm_out_str("Shutdown"); /* Should not return. */
|
---|
195 | break;
|
---|
196 | default:
|
---|
197 | SET_AH(APM_ERR_INVAL_PARAM);
|
---|
198 | SET_CF();
|
---|
199 | }
|
---|
200 | break;
|
---|
201 | case APM_DRV_VER:
|
---|
202 | AX = 0x0102; /// @todo Not right - must take driver version into account!
|
---|
203 | break;
|
---|
204 | case APM_DISCONN:
|
---|
205 | /// @todo actually perform a disconnect...
|
---|
206 | case APM_BUSY: /* Nothing to do as APM Idle doesn't slow CPU clock. */
|
---|
207 | break;
|
---|
208 | case APM_GET_EVT:
|
---|
209 | /// @todo error should be different if interface not connected + engaged
|
---|
210 | SET_AH(APM_ERR_NO_EVENTS); /* PM events don't happen. */
|
---|
211 | SET_CF();
|
---|
212 | break;
|
---|
213 | default:
|
---|
214 | BX_INFO("APM: Unsupported function AX=%04X BX=%04X called\n", AX, BX);
|
---|
215 | SET_AH(APM_ERR_UNSUPPORTED);
|
---|
216 | SET_CF();
|
---|
217 | }
|
---|
218 | }
|
---|