[CMAKE]
[reactos.git] / reactos / hal / halppc / generic / display.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 1998, 1999, 2000, 2001, 2002 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 /* $Id: display.c 23907 2006-09-04 05:52:23Z arty $
20 *
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS kernel
23 * FILE: ntoskrnl/hal/x86/display.c
24 * PURPOSE: Blue screen display
25 * PROGRAMMER: Eric Kohl
26 * UPDATE HISTORY:
27 * Created 08/10/99
28 */
29
30 /*
31 * Portions of this code are from the XFree86 Project and available from the
32 * following license:
33 *
34 * Copyright (C) 1994-2003 The XFree86 Project, Inc. All Rights Reserved.
35 *
36 * Permission is hereby granted, free of charge, to any person obtaining a copy
37 * of this software and associated documentation files (the "Software"), to
38 * deal in the Software without restriction, including without limitation the
39 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
40 * sell copies of the Software, and to permit persons to whom the Software is
41 * furnished to do so, subject to the following conditions:
42 *
43 * The above copyright notice and this permission notice shall be included in
44 * all copies or substantial portions of the Software.
45 *
46 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
47 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
48 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
49 * XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
50 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CON-
51 * NECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
52 *
53 * Except as contained in this notice, the name of the XFree86 Project shall
54 * not be used in advertising or otherwise to promote the sale, use or other
55 * dealings in this Software without prior written authorization from the
56 * XFree86 Project.
57 */
58
59 /* DISPLAY OWNERSHIP
60 *
61 * So, who owns the physical display and is allowed to write to it?
62 *
63 * In MS NT, upon boot HAL owns the display. Somewhere in the boot
64 * sequence (haven't figured out exactly where or by who), some
65 * component calls HalAcquireDisplayOwnership. From that moment on,
66 * the display is owned by that component and is switched to graphics
67 * mode. The display is not supposed to return to text mode, except
68 * in case of a bug check. The bug check will call HalDisplayString
69 * to output a string to the text screen. HAL will notice that it
70 * currently doesn't own the display and will re-take ownership, by
71 * calling the callback function passed to HalAcquireDisplayOwnership.
72 * After the bugcheck, execution is halted. So, under NT, the only
73 * possible sequence of display modes is text mode -> graphics mode ->
74 * text mode (the latter hopefully happening very infrequently).
75 *
76 * Things are a little bit different in the current state of ReactOS.
77 * We want to have a functional interactive text mode. We should be
78 * able to switch from text mode to graphics mode when a GUI app is
79 * started and switch back to text mode when it's finished. Then, when
80 * another GUI app is started, another switch to and from graphics mode
81 * is possible. Also, when the system bugchecks in graphics mode we want
82 * to switch back to text mode to show the registers and stack trace.
83 * Last but not least, HalDisplayString is used a lot more in ReactOS,
84 * e.g. to print debug messages when the /DEBUGPORT=SCREEN boot option
85 * is present.
86 * 3 Components are involved in Reactos: HAL, BLUE.SYS and VIDEOPRT.SYS.
87 * As in NT, on boot HAL owns the display. When entering the text mode
88 * command interpreter, BLUE.SYS kicks in. It will write directly to the
89 * screen, more or less behind HALs back.
90 * When a GUI app is started, WIN32K.SYS will open the DISPLAY device.
91 * This open call will end up in VIDEOPRT.SYS. That component will then
92 * take ownership of the display by calling HalAcquireDisplayOwnership.
93 * When the GUI app terminates (WIN32K.SYS will close the DISPLAY device),
94 * we want to give ownership of the display back to HAL. Using the
95 * standard exported HAL functions, that's a bit of a problem, because
96 * there is no function defined to do that. In NT, this is handled by
97 * HalDisplayString, but that solution isn't satisfactory in ReactOS,
98 * because HalDisplayString is (in some cases) also used to output debug
99 * messages. If we do it the NT way, the first debug message output while
100 * in graphics mode would switch the display back to text mode.
101 * So, instead, if HalDisplayString detects that HAL doesn't have ownership
102 * of the display, it doesn't do anything.
103 * To return ownership to HAL, a new function is exported,
104 * HalReleaseDisplayOwnership. This function is called by the DISPLAY
105 * device Close routine in VIDEOPRT.SYS. It is also called at the beginning
106 * of a bug check, so HalDisplayString is activated again.
107 * Now, while the display is in graphics mode (not owned by HAL), BLUE.SYS
108 * should also refrain from writing to the screen buffer. The text mode
109 * screen buffer might overlap the graphics mode screen buffer, so changing
110 * something in the text mode buffer might mess up the graphics screen. To
111 * allow BLUE.SYS to detect if HAL owns the display, another new function is
112 * exported, HalQueryDisplayOwnership. BLUE.SYS will call this function to
113 * check if it's allowed to touch the text mode buffer.
114 *
115 * In an ideal world, when HAL takes ownership of the display, it should set
116 * up the CRT using real-mode (actually V86 mode, but who cares) INT 0x10
117 * calls. Unfortunately, this will require HAL to setup a real-mode interrupt
118 * table etc. So, we chickened out of that by having the loader set up the
119 * display before switching to protected mode. If HAL is given back ownership
120 * after a GUI app terminates, the INT 0x10 calls are made by VIDEOPRT.SYS,
121 * since there is already support for them via the VideoPortInt10 routine.
122 */
123
124 #include <hal.h>
125 #include <rosldr.h>
126 #include <ppcboot.h>
127 #include <ppcdebug.h>
128
129 #define NDEBUG
130 #include <debug.h>
131
132 boot_infos_t PpcEarlybootInfo;
133
134 #define SCREEN_SYNCHRONIZATION
135
136 /* VARIABLES ****************************************************************/
137
138 static ULONG CursorX = 0; /* Cursor Position */
139 static ULONG CursorY = 0;
140 static ULONG SizeX = 80; /* Display size */
141 static ULONG SizeY = 25;
142
143 static BOOLEAN DisplayInitialized = FALSE;
144 static BOOLEAN HalOwnsDisplay = TRUE;
145 static ULONG GraphVideoBuffer = 0;
146 static PHAL_RESET_DISPLAY_PARAMETERS HalResetDisplayParameters = NULL;
147
148 extern UCHAR XboxFont8x16[];
149 extern void SetPhys( ULONG Addr, ULONG Data );
150 extern ULONG GetPhys( ULONG Addr );
151 extern void SetPhysByte( ULONG Addr, ULONG Data );
152
153 /* PRIVATE FUNCTIONS *********************************************************/
154
155 VOID FASTCALL
156 HalClearDisplay (UCHAR CharAttribute)
157 {
158 ULONG i;
159 ULONG deviceSize =
160 PpcEarlybootInfo.dispDeviceRowBytes *
161 PpcEarlybootInfo.dispDeviceRect[3];
162 for(i = 0; i < deviceSize; i += sizeof(int) )
163 SetPhys(GraphVideoBuffer + i, CharAttribute);
164
165 CursorX = 0;
166 CursorY = 0;
167 }
168
169
170 /* STATIC FUNCTIONS *********************************************************/
171
172 VOID STATIC
173 HalScrollDisplay (VOID)
174 {
175 ULONG i, deviceSize =
176 PpcEarlybootInfo.dispDeviceRowBytes *
177 PpcEarlybootInfo.dispDeviceRect[3];
178 ULONG Dest = (ULONG)GraphVideoBuffer,
179 Src = (ULONG)(GraphVideoBuffer + (16 * PpcEarlybootInfo.dispDeviceRowBytes));
180 ULONG End = (ULONG)
181 GraphVideoBuffer +
182 (PpcEarlybootInfo.dispDeviceRowBytes *
183 (PpcEarlybootInfo.dispDeviceRect[3]-16));
184
185 while( Src < End )
186 {
187 SetPhys((ULONG)Dest, GetPhys(Src));
188 Src += 4; Dest += 4;
189 }
190
191 /* Clear the bottom row */
192 for(i = End; i < deviceSize; i += sizeof(int) )
193 SetPhys(GraphVideoBuffer + i, 1);
194 }
195
196 VOID STATIC FASTCALL
197 HalPutCharacter (CHAR Character)
198 {
199 WRITE_PORT_UCHAR((PVOID)0x3f8, Character);
200 #if 0
201 int i,j,k;
202 ULONG Dest =
203 (GraphVideoBuffer +
204 (16 * PpcEarlybootInfo.dispDeviceRowBytes * CursorY) +
205 (8 * (PpcEarlybootInfo.dispDeviceDepth / 8) * CursorX)), RowDest;
206 UCHAR ByteToPlace;
207
208 for( i = 0; i < 16; i++ ) {
209 RowDest = Dest;
210 for( j = 0; j < 8; j++ ) {
211 ByteToPlace = ((128 >> j) & (XboxFont8x16[(16 * Character) + i])) ? 0xff : 1;
212 for( k = 0; k < PpcEarlybootInfo.dispDeviceDepth / 8; k++, RowDest++ ) {
213 SetPhysByte(RowDest, ByteToPlace);
214 }
215 }
216 Dest += PpcEarlybootInfo.dispDeviceRowBytes;
217 }
218 #endif
219 }
220
221 /* PRIVATE FUNCTIONS ********************************************************/
222
223 VOID FASTCALL
224 HalInitializeDisplay (PROS_LOADER_PARAMETER_BLOCK LoaderBlock)
225 /*
226 * FUNCTION: Initalize the display
227 * ARGUMENTS:
228 * InitParameters = Parameters setup by the boot loader
229 */
230 {
231 if (! DisplayInitialized)
232 {
233 boot_infos_t *XBootInfo = (boot_infos_t *)LoaderBlock->ArchExtra;
234 GraphVideoBuffer = (ULONG)XBootInfo->dispDeviceBase;
235 memcpy(&PpcEarlybootInfo, XBootInfo, sizeof(*XBootInfo));
236
237 /* Set cursor position */
238 CursorX = 0;
239 CursorY = 0;
240
241 SizeX = XBootInfo->dispDeviceRowBytes / XBootInfo->dispDeviceDepth;
242 SizeY = XBootInfo->dispDeviceRect[3] / 16;
243
244 HalClearDisplay(1);
245
246 DisplayInitialized = TRUE;
247 }
248 }
249
250
251 /* PUBLIC FUNCTIONS *********************************************************/
252
253 VOID NTAPI
254 HalReleaseDisplayOwnership(VOID)
255 /*
256 * FUNCTION: Release ownership of display back to HAL
257 */
258 {
259 if (HalResetDisplayParameters == NULL)
260 return;
261
262 if (HalOwnsDisplay == TRUE)
263 return;
264
265 HalOwnsDisplay = TRUE;
266 HalClearDisplay(0);
267 }
268
269
270 VOID NTAPI
271 HalAcquireDisplayOwnership(IN PHAL_RESET_DISPLAY_PARAMETERS ResetDisplayParameters)
272 /*
273 * FUNCTION:
274 * ARGUMENTS:
275 * ResetDisplayParameters = Pointer to a driver specific
276 * reset routine.
277 */
278 {
279 HalOwnsDisplay = FALSE;
280 HalResetDisplayParameters = ResetDisplayParameters;
281 }
282
283 VOID NTAPI
284 HalDisplayString(IN PCH String)
285 /*
286 * FUNCTION: Switches the screen to HAL console mode (BSOD) if not there
287 * already and displays a string
288 * ARGUMENT:
289 * string = ASCII string to display
290 * NOTE: Use with care because there is no support for returning from BSOD
291 * mode
292 */
293 {
294 PCH pch;
295 //static KSPIN_LOCK Lock;
296 KIRQL OldIrql;
297 BOOLEAN InterruptsEnabled = __readmsr();
298
299 /* See comment at top of file */
300 if (! HalOwnsDisplay || ! DisplayInitialized)
301 {
302 return;
303 }
304
305 pch = String;
306
307 KeRaiseIrql(HIGH_LEVEL, &OldIrql);
308 //KiAcquireSpinLock(&Lock);
309
310 _disable();
311
312 while (*pch != 0)
313 {
314 if (*pch == '\n')
315 {
316 CursorY++;
317 CursorX = 0;
318 }
319 else if (*pch == '\b')
320 {
321 if (CursorX > 0)
322 {
323 CursorX--;
324 }
325 }
326 else if (*pch != '\r')
327 {
328 HalPutCharacter (*pch);
329 CursorX++;
330
331 if (CursorX >= SizeX)
332 {
333 CursorY++;
334 CursorX = 0;
335 }
336 }
337
338 if (CursorY >= SizeY)
339 {
340 HalScrollDisplay ();
341 CursorY = SizeY - 1;
342 }
343
344 pch++;
345 }
346
347 __writemsr(InterruptsEnabled);
348
349 //KiReleaseSpinLock(&Lock);
350 KeLowerIrql(OldIrql);
351 }
352
353 VOID NTAPI
354 HalQueryDisplayParameters(OUT PULONG DispSizeX,
355 OUT PULONG DispSizeY,
356 OUT PULONG CursorPosX,
357 OUT PULONG CursorPosY)
358 {
359 if (DispSizeX)
360 *DispSizeX = SizeX;
361 if (DispSizeY)
362 *DispSizeY = SizeY;
363 if (CursorPosX)
364 *CursorPosX = CursorX;
365 if (CursorPosY)
366 *CursorPosY = CursorY;
367 }
368
369
370 VOID NTAPI
371 HalSetDisplayParameters(IN ULONG CursorPosX,
372 IN ULONG CursorPosY)
373 {
374 CursorX = (CursorPosX < SizeX) ? CursorPosX : SizeX - 1;
375 CursorY = (CursorPosY < SizeY) ? CursorPosY : SizeY - 1;
376 }
377
378
379 BOOLEAN NTAPI
380 HalQueryDisplayOwnership(VOID)
381 {
382 return !HalOwnsDisplay;
383 }
384
385 /* EOF */