2011-08-08 09:18:39 -04:00
|
|
|
/******************************************************************************
|
|
|
|
|
|
|
|
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011.
|
|
|
|
|
|
|
|
This file is part of Sortix.
|
|
|
|
|
|
|
|
Sortix is free software: you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with Sortix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
vga.h
|
|
|
|
A Video Graphics Array driver.
|
|
|
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "platform.h"
|
|
|
|
#include <libmaxsi/memory.h>
|
|
|
|
#include "vga.h"
|
|
|
|
#include "memorymanagement.h"
|
|
|
|
#include "scheduler.h"
|
2011-10-26 18:05:20 -04:00
|
|
|
#include "syscall.h"
|
2011-09-21 14:52:29 -04:00
|
|
|
#include "process.h"
|
2011-11-20 09:58:42 -05:00
|
|
|
#include "serialterminal.h"
|
2011-08-08 09:18:39 -04:00
|
|
|
|
|
|
|
using namespace Maxsi;
|
|
|
|
|
|
|
|
namespace Sortix
|
|
|
|
{
|
|
|
|
namespace VGA
|
|
|
|
{
|
2011-10-26 18:05:20 -04:00
|
|
|
addr_t SysCreateFrame();
|
|
|
|
int SysChangeFrame(int fd);
|
|
|
|
int SysDeleteFrame(int fd);
|
|
|
|
|
2011-08-08 09:18:39 -04:00
|
|
|
uint16_t* const vga = (uint16_t* const) 0xB8000;
|
2011-08-22 05:11:50 -04:00
|
|
|
const int width = 80;
|
|
|
|
const int height = 80;
|
2011-08-08 09:18:39 -04:00
|
|
|
|
|
|
|
DevVGAFrame* currentframe;
|
|
|
|
|
|
|
|
void Init()
|
|
|
|
{
|
|
|
|
currentframe = NULL;
|
2011-10-26 18:05:20 -04:00
|
|
|
|
|
|
|
Syscall::Register(SYSCALL_CREATE_FRAME, (void*) SysCreateFrame);
|
|
|
|
Syscall::Register(SYSCALL_CHANGE_FRAME, (void*) SysChangeFrame);
|
|
|
|
Syscall::Register(SYSCALL_DELETE_FRAME, (void*) SysDeleteFrame);
|
2011-08-08 09:18:39 -04:00
|
|
|
}
|
|
|
|
|
2011-08-22 05:11:50 -04:00
|
|
|
// Changes the position of the hardware cursor.
|
|
|
|
void SetCursor(nat x, nat y)
|
|
|
|
{
|
|
|
|
nat value = x + y * width;
|
|
|
|
|
|
|
|
// This sends a command to indicies 14 and 15 in the
|
|
|
|
// CRT Control Register of the VGA controller. These
|
|
|
|
// are the high and low bytes of the index that show
|
|
|
|
// where the hardware cursor is to be 'blinking'.
|
|
|
|
CPU::OutPortB(0x3D4, 14);
|
|
|
|
CPU::OutPortB(0x3D5, (value >> 8) & 0xFF);
|
|
|
|
CPU::OutPortB(0x3D4, 15);
|
|
|
|
CPU::OutPortB(0x3D5, (value >> 0) & 0xFF);
|
|
|
|
}
|
|
|
|
|
2011-10-26 18:05:20 -04:00
|
|
|
addr_t SysCreateFrame()
|
2011-08-08 09:18:39 -04:00
|
|
|
{
|
|
|
|
addr_t page = Page::Get();
|
2011-10-26 18:05:20 -04:00
|
|
|
if ( !page ) { return 0; }
|
2011-08-08 09:18:39 -04:00
|
|
|
|
|
|
|
Process* process = CurrentProcess();
|
2011-09-21 14:52:29 -04:00
|
|
|
addr_t mapto = process->AllocVirtualAddr(0x1000UL);
|
2011-08-08 09:18:39 -04:00
|
|
|
UserFrame* userframe = (UserFrame*) mapto;
|
|
|
|
|
|
|
|
// TODO: Check if mapto collides with any other memory section!
|
|
|
|
|
2011-10-02 09:58:08 -04:00
|
|
|
if ( !Memory::MapUser(page, mapto) )
|
2011-08-08 09:18:39 -04:00
|
|
|
{
|
2011-10-26 18:05:20 -04:00
|
|
|
Page::Put(page); return 0;
|
2011-08-08 09:18:39 -04:00
|
|
|
}
|
|
|
|
|
2011-10-02 09:58:08 -04:00
|
|
|
Maxsi::Memory::Set(userframe, 0, sizeof(UserFrame));
|
2011-08-08 09:18:39 -04:00
|
|
|
|
|
|
|
DevVGAFrame* frame = new DevVGAFrame();
|
|
|
|
if ( frame == NULL )
|
|
|
|
{
|
2011-10-02 09:58:08 -04:00
|
|
|
Memory::UnmapUser(mapto);
|
2011-10-26 18:05:20 -04:00
|
|
|
Page::Put(page); return 0;
|
2011-08-08 09:18:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int fd = process->descriptors.Allocate(frame);
|
|
|
|
if ( fd < 0 )
|
|
|
|
{
|
|
|
|
delete frame;
|
2011-10-02 09:58:08 -04:00
|
|
|
Memory::UnmapUser(mapto);
|
2011-10-26 18:05:20 -04:00
|
|
|
Page::Put(page); return 0;
|
2011-08-08 09:18:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
userframe->fd = fd;
|
|
|
|
|
|
|
|
frame->process = process;
|
|
|
|
frame->physical = page;
|
|
|
|
frame->userframe = userframe;
|
|
|
|
|
2011-10-26 18:05:20 -04:00
|
|
|
return mapto;
|
2011-08-08 09:18:39 -04:00
|
|
|
}
|
|
|
|
|
2011-10-26 18:05:20 -04:00
|
|
|
int SysChangeFrame(int fd)
|
2011-08-08 09:18:39 -04:00
|
|
|
{
|
|
|
|
Process* process = CurrentProcess();
|
|
|
|
Device* device = process->descriptors.Get(fd);
|
2011-10-26 18:05:20 -04:00
|
|
|
if ( !device ) { return -1; }
|
2011-08-08 09:18:39 -04:00
|
|
|
|
2011-10-26 18:05:20 -04:00
|
|
|
if ( !device->IsType(Device::VGABUFFER) ) { return -2; }
|
2011-08-08 09:18:39 -04:00
|
|
|
|
|
|
|
DevVGAFrame* frame = (DevVGAFrame*) device;
|
|
|
|
|
|
|
|
ASSERT(frame->process == process);
|
|
|
|
ASSERT(frame->physical != 0);
|
|
|
|
ASSERT(frame->userframe != NULL);
|
|
|
|
ASSERT(frame->onscreen == (frame == currentframe));
|
|
|
|
|
|
|
|
// TODO: Check if userframe is actually user-space writable!
|
|
|
|
|
|
|
|
// Check if we need to do anything.
|
2011-10-26 18:05:20 -04:00
|
|
|
if ( frame == currentframe ) { return 0; }
|
2011-08-08 09:18:39 -04:00
|
|
|
|
|
|
|
// If there is already a currently used frame? If so, swap it from
|
|
|
|
// the VGA memory and back to the RAM. This should be done
|
|
|
|
// transparently such that the program doesn't feel the difference.
|
|
|
|
if ( currentframe != NULL )
|
|
|
|
{
|
|
|
|
ASSERT(currentframe->physical != frame->physical);
|
|
|
|
ASSERT(currentframe->userframe != frame->userframe);
|
|
|
|
ASSERT(currentframe->onscreen == true);
|
|
|
|
|
|
|
|
if ( currentframe->process != process )
|
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
Memory::SwitchAddressSpace(currentframe->process->addrspace);
|
2011-08-08 09:18:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remap the pages in the owning process.
|
|
|
|
// TODO: Check if userframe is actually user-space writable!
|
2011-10-02 09:58:08 -04:00
|
|
|
Memory::UnmapUser((addr_t) currentframe->userframe);
|
|
|
|
Memory::MapUser(currentframe->physical, (addr_t) currentframe->userframe);
|
|
|
|
Memory::InvalidatePage((addr_t) frame->userframe);
|
2011-08-08 09:18:39 -04:00
|
|
|
|
|
|
|
// Restore the contents of this frame to the VGA framebuffer.
|
2011-10-02 09:58:08 -04:00
|
|
|
Maxsi::Memory::Copy(currentframe->userframe, vga, sizeof(UserFrame));
|
2011-08-08 09:18:39 -04:00
|
|
|
|
|
|
|
if ( currentframe->process != process )
|
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
Memory::SwitchAddressSpace(process->addrspace);
|
2011-08-08 09:18:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
currentframe->onscreen = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now move the contents of this frame to the VGA framebuffer.
|
2011-10-02 09:58:08 -04:00
|
|
|
Maxsi::Memory::Copy(vga, frame->userframe, sizeof(UserFrame));
|
2011-08-08 09:18:39 -04:00
|
|
|
|
|
|
|
// Remap the pages such that the current process now uses the vga.
|
2011-10-02 09:58:08 -04:00
|
|
|
Memory::UnmapUser((addr_t) frame->userframe);
|
|
|
|
Memory::MapUser((addr_t) vga, (addr_t) frame->userframe);
|
|
|
|
Memory::InvalidatePage((addr_t) frame->userframe);
|
2011-08-08 09:18:39 -04:00
|
|
|
|
|
|
|
frame->onscreen = true;
|
2011-08-22 05:11:50 -04:00
|
|
|
currentframe = frame;
|
2011-09-08 15:09:14 -04:00
|
|
|
SetCursor(width, height-1);
|
2011-10-26 18:05:20 -04:00
|
|
|
|
|
|
|
return 0;
|
2011-08-08 09:18:39 -04:00
|
|
|
}
|
|
|
|
|
2011-10-26 18:05:20 -04:00
|
|
|
int SysDeleteFrame(int fd)
|
2011-08-08 09:18:39 -04:00
|
|
|
{
|
|
|
|
Process* process = CurrentProcess();
|
|
|
|
Device* device = process->descriptors.Get(fd);
|
|
|
|
process->descriptors.Free(fd);
|
|
|
|
|
2011-10-26 18:05:20 -04:00
|
|
|
if ( device == NULL ) { return -1; }
|
2011-11-16 02:37:29 -05:00
|
|
|
device->Unref();
|
2011-10-26 18:05:20 -04:00
|
|
|
|
|
|
|
return 0;
|
2011-08-08 09:18:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DevVGAFrame::DevVGAFrame()
|
|
|
|
{
|
|
|
|
process = NULL;
|
|
|
|
userframe = NULL;
|
|
|
|
physical = 0;
|
|
|
|
onscreen = false;
|
2011-11-20 09:58:42 -05:00
|
|
|
SerialTerminal::OnVGAFrameCreated();
|
2011-08-08 09:18:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
DevVGAFrame::~DevVGAFrame()
|
|
|
|
{
|
2011-11-20 09:58:42 -05:00
|
|
|
SerialTerminal::OnVGAFrameDeleted();
|
2011-08-08 09:18:39 -04:00
|
|
|
if ( process != NULL ) { ASSERT(CurrentProcess() == process); }
|
2011-10-02 09:58:08 -04:00
|
|
|
if ( userframe != NULL ) { Memory::UnmapUser((addr_t) userframe); Memory::InvalidatePage((addr_t) userframe); }
|
2011-08-08 09:18:39 -04:00
|
|
|
if ( physical != 0 ) { Page::Put(physical); }
|
2011-11-20 18:27:10 -05:00
|
|
|
if ( VGA::currentframe == this ) { VGA::currentframe = NULL; }
|
2011-08-08 09:18:39 -04:00
|
|
|
}
|
|
|
|
|
2011-11-16 02:37:29 -05:00
|
|
|
bool DevVGAFrame::IsType(unsigned type)
|
|
|
|
{
|
|
|
|
return type == Device::VGABUFFER;
|
|
|
|
}
|
2011-08-08 09:18:39 -04:00
|
|
|
}
|