3 * Copyright (C) 1998, 1999, 2000, 2001, 2002 ReactOS Team
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.
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.
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.
19 /* $Id: display.c 23907 2006-09-04 05:52:23Z arty $
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
31 * Portions of this code are from the XFree86 Project and available from the
34 * Copyright (C) 1994-2003 The XFree86 Project, Inc. All Rights Reserved.
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:
43 * The above copyright notice and this permission notice shall be included in
44 * all copies or substantial portions of the Software.
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.
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
61 * So, who owns the physical display and is allowed to write to it?
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).
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
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.
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.
126 #include <ppcdebug.h>
131 boot_infos_t PpcEarlybootInfo
;
133 #define SCREEN_SYNCHRONIZATION
135 /* VARIABLES ****************************************************************/
137 static ULONG CursorX
= 0; /* Cursor Position */
138 static ULONG CursorY
= 0;
139 static ULONG SizeX
= 80; /* Display size */
140 static ULONG SizeY
= 25;
142 static BOOLEAN DisplayInitialized
= FALSE
;
143 static BOOLEAN HalOwnsDisplay
= TRUE
;
144 static ULONG GraphVideoBuffer
= 0;
145 static PHAL_RESET_DISPLAY_PARAMETERS HalResetDisplayParameters
= NULL
;
147 extern UCHAR XboxFont8x16
[];
148 extern void SetPhys( ULONG Addr
, ULONG Data
);
149 extern ULONG
GetPhys( ULONG Addr
);
150 extern void SetPhysByte( ULONG Addr
, ULONG Data
);
152 /* PRIVATE FUNCTIONS *********************************************************/
155 HalClearDisplay (UCHAR CharAttribute
)
159 PpcEarlybootInfo
.dispDeviceRowBytes
*
160 PpcEarlybootInfo
.dispDeviceRect
[3];
161 for(i
= 0; i
< deviceSize
; i
+= sizeof(int) )
162 SetPhys(GraphVideoBuffer
+ i
, CharAttribute
);
169 /* STATIC FUNCTIONS *********************************************************/
172 HalScrollDisplay (VOID
)
174 ULONG i
, deviceSize
=
175 PpcEarlybootInfo
.dispDeviceRowBytes
*
176 PpcEarlybootInfo
.dispDeviceRect
[3];
177 ULONG Dest
= (ULONG
)GraphVideoBuffer
,
178 Src
= (ULONG
)(GraphVideoBuffer
+ (16 * PpcEarlybootInfo
.dispDeviceRowBytes
));
181 (PpcEarlybootInfo
.dispDeviceRowBytes
*
182 (PpcEarlybootInfo
.dispDeviceRect
[3]-16));
186 SetPhys((ULONG
)Dest
, GetPhys(Src
));
190 /* Clear the bottom row */
191 for(i
= End
; i
< deviceSize
; i
+= sizeof(int) )
192 SetPhys(GraphVideoBuffer
+ i
, 1);
196 HalPutCharacter (CHAR Character
)
198 WRITE_PORT_UCHAR((PVOID
)0x3f8, Character
);
203 (16 * PpcEarlybootInfo
.dispDeviceRowBytes
* CursorY
) +
204 (8 * (PpcEarlybootInfo
.dispDeviceDepth
/ 8) * CursorX
)), RowDest
;
207 for( i
= 0; i
< 16; i
++ ) {
209 for( j
= 0; j
< 8; j
++ ) {
210 ByteToPlace
= ((128 >> j
) & (XboxFont8x16
[(16 * Character
) + i
])) ? 0xff : 1;
211 for( k
= 0; k
< PpcEarlybootInfo
.dispDeviceDepth
/ 8; k
++, RowDest
++ ) {
212 SetPhysByte(RowDest
, ByteToPlace
);
215 Dest
+= PpcEarlybootInfo
.dispDeviceRowBytes
;
220 /* PRIVATE FUNCTIONS ********************************************************/
223 HalInitializeDisplay (PROS_LOADER_PARAMETER_BLOCK LoaderBlock
)
225 * FUNCTION: Initalize the display
227 * InitParameters = Parameters setup by the boot loader
230 if (! DisplayInitialized
)
232 boot_infos_t
*XBootInfo
= (boot_infos_t
*)LoaderBlock
->ArchExtra
;
233 GraphVideoBuffer
= (ULONG
)XBootInfo
->dispDeviceBase
;
234 memcpy(&PpcEarlybootInfo
, XBootInfo
, sizeof(*XBootInfo
));
236 /* Set cursor position */
240 SizeX
= XBootInfo
->dispDeviceRowBytes
/ XBootInfo
->dispDeviceDepth
;
241 SizeY
= XBootInfo
->dispDeviceRect
[3] / 16;
245 DisplayInitialized
= TRUE
;
250 /* PUBLIC FUNCTIONS *********************************************************/
253 HalReleaseDisplayOwnership(VOID
)
255 * FUNCTION: Release ownership of display back to HAL
258 if (HalResetDisplayParameters
== NULL
)
261 if (HalOwnsDisplay
== TRUE
)
264 HalOwnsDisplay
= TRUE
;
270 HalAcquireDisplayOwnership(IN PHAL_RESET_DISPLAY_PARAMETERS ResetDisplayParameters
)
274 * ResetDisplayParameters = Pointer to a driver specific
278 HalOwnsDisplay
= FALSE
;
279 HalResetDisplayParameters
= ResetDisplayParameters
;
283 HalDisplayString(IN PCH String
)
285 * FUNCTION: Switches the screen to HAL console mode (BSOD) if not there
286 * already and displays a string
288 * string = ASCII string to display
289 * NOTE: Use with care because there is no support for returning from BSOD
294 //static KSPIN_LOCK Lock;
296 BOOLEAN InterruptsEnabled
= __readmsr();
298 /* See comment at top of file */
299 if (! HalOwnsDisplay
|| ! DisplayInitialized
)
306 KeRaiseIrql(HIGH_LEVEL
, &OldIrql
);
307 //KiAcquireSpinLock(&Lock);
318 else if (*pch
== '\b')
325 else if (*pch
!= '\r')
327 HalPutCharacter (*pch
);
330 if (CursorX
>= SizeX
)
337 if (CursorY
>= SizeY
)
346 __writemsr(InterruptsEnabled
);
348 //KiReleaseSpinLock(&Lock);
349 KeLowerIrql(OldIrql
);
353 HalQueryDisplayParameters(OUT PULONG DispSizeX
,
354 OUT PULONG DispSizeY
,
355 OUT PULONG CursorPosX
,
356 OUT PULONG CursorPosY
)
363 *CursorPosX
= CursorX
;
365 *CursorPosY
= CursorY
;
370 HalSetDisplayParameters(IN ULONG CursorPosX
,
373 CursorX
= (CursorPosX
< SizeX
) ? CursorPosX
: SizeX
- 1;
374 CursorY
= (CursorPosY
< SizeY
) ? CursorPosY
: SizeY
- 1;
379 HalQueryDisplayOwnership(VOID
)
381 return !HalOwnsDisplay
;