Create a branch for working on csrss and co.
[reactos.git] / win32ss / drivers / videoprt / int10.c
1 /*
2 * VideoPort driver
3 *
4 * Copyright (C) 2002, 2003, 2004 ReactOS Team
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22 #include "videoprt.h"
23
24 /* PRIVATE FUNCTIONS **********************************************************/
25
26 #if defined(_M_IX86)
27 VP_STATUS NTAPI
28 IntInt10AllocateBuffer(
29 IN PVOID Context,
30 OUT PUSHORT Seg,
31 OUT PUSHORT Off,
32 IN OUT PULONG Length)
33 {
34 PVOID MemoryAddress;
35 NTSTATUS Status;
36 PKPROCESS CallingProcess = (PKPROCESS)PsGetCurrentProcess();
37 KAPC_STATE ApcState;
38
39 TRACE_(VIDEOPRT, "IntInt10AllocateBuffer\n");
40
41 IntAttachToCSRSS(&CallingProcess, &ApcState);
42
43 MemoryAddress = (PVOID)0x20000;
44 Status = ZwAllocateVirtualMemory(NtCurrentProcess(), &MemoryAddress, 0,
45 Length, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
46
47 if (!NT_SUCCESS(Status))
48 {
49 WARN_(VIDEOPRT, "- ZwAllocateVirtualMemory failed\n");
50 IntDetachFromCSRSS(&CallingProcess, &ApcState);
51 return ERROR_NOT_ENOUGH_MEMORY;
52 }
53
54 if (MemoryAddress > (PVOID)(0x100000 - *Length))
55 {
56 ZwFreeVirtualMemory(NtCurrentProcess(), &MemoryAddress, Length,
57 MEM_RELEASE);
58 WARN_(VIDEOPRT, "- Unacceptable memory allocated\n");
59 IntDetachFromCSRSS(&CallingProcess, &ApcState);
60 return ERROR_NOT_ENOUGH_MEMORY;
61 }
62
63 *Seg = (USHORT)((ULONG)MemoryAddress >> 4);
64 *Off = (USHORT)((ULONG)MemoryAddress & 0xF);
65
66 INFO_(VIDEOPRT, "- Segment: %x\n", (ULONG)MemoryAddress >> 4);
67 INFO_(VIDEOPRT, "- Offset: %x\n", (ULONG)MemoryAddress & 0xF);
68 INFO_(VIDEOPRT, "- Length: %x\n", *Length);
69
70 IntDetachFromCSRSS(&CallingProcess, &ApcState);
71
72 return NO_ERROR;
73 }
74
75 VP_STATUS NTAPI
76 IntInt10FreeBuffer(
77 IN PVOID Context,
78 IN USHORT Seg,
79 IN USHORT Off)
80 {
81 PVOID MemoryAddress = (PVOID)((Seg << 4) | Off);
82 NTSTATUS Status;
83 PKPROCESS CallingProcess = (PKPROCESS)PsGetCurrentProcess();
84 KAPC_STATE ApcState;
85 SIZE_T Size = 0;
86
87 TRACE_(VIDEOPRT, "IntInt10FreeBuffer\n");
88 INFO_(VIDEOPRT, "- Segment: %x\n", Seg);
89 INFO_(VIDEOPRT, "- Offset: %x\n", Off);
90
91 IntAttachToCSRSS(&CallingProcess, &ApcState);
92 Status = ZwFreeVirtualMemory(NtCurrentProcess(), &MemoryAddress, &Size,
93 MEM_RELEASE);
94 IntDetachFromCSRSS(&CallingProcess, &ApcState);
95
96 return Status;
97 }
98
99 VP_STATUS NTAPI
100 IntInt10ReadMemory(
101 IN PVOID Context,
102 IN USHORT Seg,
103 IN USHORT Off,
104 OUT PVOID Buffer,
105 IN ULONG Length)
106 {
107 PKPROCESS CallingProcess = (PKPROCESS)PsGetCurrentProcess();
108 KAPC_STATE ApcState;
109
110 TRACE_(VIDEOPRT, "IntInt10ReadMemory\n");
111 INFO_(VIDEOPRT, "- Segment: %x\n", Seg);
112 INFO_(VIDEOPRT, "- Offset: %x\n", Off);
113 INFO_(VIDEOPRT, "- Buffer: %x\n", Buffer);
114 INFO_(VIDEOPRT, "- Length: %x\n", Length);
115
116 IntAttachToCSRSS(&CallingProcess, &ApcState);
117 RtlCopyMemory(Buffer, (PVOID)((Seg << 4) | Off), Length);
118 IntDetachFromCSRSS(&CallingProcess, &ApcState);
119
120 return NO_ERROR;
121 }
122
123 VP_STATUS NTAPI
124 IntInt10WriteMemory(
125 IN PVOID Context,
126 IN USHORT Seg,
127 IN USHORT Off,
128 IN PVOID Buffer,
129 IN ULONG Length)
130 {
131 PKPROCESS CallingProcess = (PKPROCESS)PsGetCurrentProcess();
132 KAPC_STATE ApcState;
133
134 TRACE_(VIDEOPRT, "IntInt10WriteMemory\n");
135 INFO_(VIDEOPRT, "- Segment: %x\n", Seg);
136 INFO_(VIDEOPRT, "- Offset: %x\n", Off);
137 INFO_(VIDEOPRT, "- Buffer: %x\n", Buffer);
138 INFO_(VIDEOPRT, "- Length: %x\n", Length);
139
140 IntAttachToCSRSS(&CallingProcess, &ApcState);
141 RtlCopyMemory((PVOID)((Seg << 4) | Off), Buffer, Length);
142 IntDetachFromCSRSS(&CallingProcess, &ApcState);
143
144 return NO_ERROR;
145 }
146
147 VP_STATUS
148 NTAPI
149 IntInt10CallBios(
150 IN PVOID Context,
151 IN OUT PINT10_BIOS_ARGUMENTS BiosArguments)
152 {
153 CONTEXT BiosContext;
154 NTSTATUS Status;
155 PKPROCESS CallingProcess = (PKPROCESS)PsGetCurrentProcess();
156 KAPC_STATE ApcState;
157
158 /* Attach to CSRSS */
159 IntAttachToCSRSS(&CallingProcess, &ApcState);
160
161 /* Clear the context */
162 RtlZeroMemory(&BiosContext, sizeof(CONTEXT));
163
164 /* Fill out the bios arguments */
165 BiosContext.Eax = BiosArguments->Eax;
166 BiosContext.Ebx = BiosArguments->Ebx;
167 BiosContext.Ecx = BiosArguments->Ecx;
168 BiosContext.Edx = BiosArguments->Edx;
169 BiosContext.Esi = BiosArguments->Esi;
170 BiosContext.Edi = BiosArguments->Edi;
171 BiosContext.Ebp = BiosArguments->Ebp;
172 BiosContext.SegDs = BiosArguments->SegDs;
173 BiosContext.SegEs = BiosArguments->SegEs;
174
175 /* Do the ROM BIOS call */
176 Status = Ke386CallBios(0x10, &BiosContext);
177
178 /* Return the arguments */
179 BiosArguments->Eax = BiosContext.Eax;
180 BiosArguments->Ebx = BiosContext.Ebx;
181 BiosArguments->Ecx = BiosContext.Ecx;
182 BiosArguments->Edx = BiosContext.Edx;
183 BiosArguments->Esi = BiosContext.Esi;
184 BiosArguments->Edi = BiosContext.Edi;
185 BiosArguments->Ebp = BiosContext.Ebp;
186 BiosArguments->SegDs = (USHORT)BiosContext.SegDs;
187 BiosArguments->SegEs = (USHORT)BiosContext.SegEs;
188
189 /* Detach and return status */
190 IntDetachFromCSRSS(&CallingProcess, &ApcState);
191 if (NT_SUCCESS(Status)) return NO_ERROR;
192 return ERROR_INVALID_PARAMETER;
193 }
194 #endif
195
196 /* PUBLIC FUNCTIONS ***********************************************************/
197
198 /*
199 * @implemented
200 */
201
202 VP_STATUS NTAPI
203 VideoPortInt10(
204 IN PVOID HwDeviceExtension,
205 IN PVIDEO_X86_BIOS_ARGUMENTS BiosArguments)
206 {
207 #if defined(_M_IX86)
208 CONTEXT BiosContext;
209 NTSTATUS Status;
210 PKPROCESS CallingProcess = (PKPROCESS)PsGetCurrentProcess();
211 KAPC_STATE ApcState;
212
213 if (!CsrssInitialized)
214 {
215 return ERROR_INVALID_PARAMETER;
216 }
217
218 /* Attach to CSRSS */
219 IntAttachToCSRSS(&CallingProcess, &ApcState);
220
221 /* Clear the context */
222 RtlZeroMemory(&BiosContext, sizeof(CONTEXT));
223
224 /* Fill out the bios arguments */
225 BiosContext.Eax = BiosArguments->Eax;
226 BiosContext.Ebx = BiosArguments->Ebx;
227 BiosContext.Ecx = BiosArguments->Ecx;
228 BiosContext.Edx = BiosArguments->Edx;
229 BiosContext.Esi = BiosArguments->Esi;
230 BiosContext.Edi = BiosArguments->Edi;
231 BiosContext.Ebp = BiosArguments->Ebp;
232
233 /* Do the ROM BIOS call */
234 Status = Ke386CallBios(0x10, &BiosContext);
235
236 /* Return the arguments */
237 BiosArguments->Eax = BiosContext.Eax;
238 BiosArguments->Ebx = BiosContext.Ebx;
239 BiosArguments->Ecx = BiosContext.Ecx;
240 BiosArguments->Edx = BiosContext.Edx;
241 BiosArguments->Esi = BiosContext.Esi;
242 BiosArguments->Edi = BiosContext.Edi;
243 BiosArguments->Ebp = BiosContext.Ebp;
244
245 /* Detach from CSRSS */
246 IntDetachFromCSRSS(&CallingProcess, &ApcState);
247 if (NT_SUCCESS(Status)) return NO_ERROR;
248 return ERROR_INVALID_PARAMETER;
249 #else
250 /* Not implemented for anything else than X86*/
251 DPRINT1("Int10 not available on non-x86!\n");
252 return ERROR_INVALID_FUNCTION;
253 #endif
254 }