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