2001-10-03 17:10:38 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2008-04-30 02:14:23 +04:00
|
|
|
// $Id: load32bitOShack.cc,v 1.29 2008-04-29 22:14:23 sshwarts Exp $
|
2001-10-03 17:10:38 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2001-04-10 06:20:02 +04:00
|
|
|
// Copyright (C) 2001 MandrakeSoft S.A.
|
2001-04-10 05:04:59 +04:00
|
|
|
//
|
|
|
|
// MandrakeSoft S.A.
|
|
|
|
// 43, rue d'Aboukir
|
|
|
|
// 75002 Paris - France
|
|
|
|
// http://www.linux-mandrake.com/
|
|
|
|
// http://www.mandrakesoft.com/
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library 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
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
|
|
|
|
#include "bochs.h"
|
2006-03-07 01:03:16 +03:00
|
|
|
#include "cpu/cpu.h"
|
2004-06-19 19:20:15 +04:00
|
|
|
#include "iodev/iodev.h"
|
merge in BRANCH-io-cleanup.
To see the commit logs for this use either cvsweb or
cvs update -r BRANCH-io-cleanup and then 'cvs log' the various files.
In general this provides a generic interface for logging.
logfunctions:: is a class that is inherited by some classes, and also
. allocated as a standalone global called 'genlog'. All logging uses
. one of the ::info(), ::error(), ::ldebug(), ::panic() methods of this
. class through 'BX_INFO(), BX_ERROR(), BX_DEBUG(), BX_PANIC()' macros
. respectively.
.
. An example usage:
. BX_INFO(("Hello, World!\n"));
iofunctions:: is a class that is allocated once by default, and assigned
as the iofunction of each logfunctions instance. It is this class that
maintains the file descriptor and other output related code, at this
point using vfprintf(). At some future point, someone may choose to
write a gui 'console' for bochs to which messages would be redirected
simply by assigning a different iofunction class to the various logfunctions
objects.
More cleanup is coming, but this works for now. If you want to see alot
of debugging output, in main.cc, change onoff[LOGLEV_DEBUG]=0 to =1.
Comments, bugs, flames, to me: todd@fries.net
2001-05-15 18:49:57 +04:00
|
|
|
#define LOG_THIS genlog->
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
|
|
|
|
static void bx_load_linux_hack(void);
|
|
|
|
static void bx_load_null_kernel_hack(void);
|
|
|
|
static Bit32u bx_load_kernel_image(char *path, Bit32u paddr);
|
|
|
|
|
2006-03-07 01:03:16 +03:00
|
|
|
void bx_load32bitOSimagehack(void)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2006-02-24 01:48:57 +03:00
|
|
|
if (SIM->get_param_string(BXPN_LOAD32BITOS_IOLOG) &&
|
2006-03-07 01:03:16 +03:00
|
|
|
(SIM->get_param_string(BXPN_LOAD32BITOS_IOLOG)->getptr()[0] != '\0'))
|
|
|
|
{
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2006-03-07 01:03:16 +03:00
|
|
|
// Replay IO from log to initialize IO devices to
|
|
|
|
// a reasonable state needed for the OS. This is done
|
|
|
|
// in lieu of running the 16-bit BIOS to init things,
|
|
|
|
// since we want to test straight 32bit stuff for
|
|
|
|
// freemware.
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2006-03-07 01:03:16 +03:00
|
|
|
FILE *fp = fopen(SIM->get_param_string(BXPN_LOAD32BITOS_IOLOG)->getptr(), "r");
|
|
|
|
if (fp == NULL) {
|
|
|
|
BX_PANIC(("could not open IO init file."));
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2006-03-07 01:03:16 +03:00
|
|
|
while (1) {
|
|
|
|
unsigned len, op, port, val;
|
|
|
|
int ret;
|
|
|
|
ret = fscanf(fp, "%u %u %x %x\n", &len, &op, &port, &val);
|
|
|
|
if (ret != 4) {
|
|
|
|
BX_PANIC(("could not open IO init file."));
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
2006-03-07 01:03:16 +03:00
|
|
|
if (op == 0) {
|
|
|
|
// read
|
|
|
|
bx_devices.inp(port, len);
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
2006-03-07 01:03:16 +03:00
|
|
|
else if (op == 1) {
|
|
|
|
// write
|
|
|
|
bx_devices.outp(port, val, len);
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
2006-03-07 01:03:16 +03:00
|
|
|
else {
|
|
|
|
BX_PANIC(("bad IO op in init filen"));
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
2006-03-07 01:03:16 +03:00
|
|
|
if (feof(fp)) break;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
2006-03-07 01:03:16 +03:00
|
|
|
} //if iolog file to load
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
// Invoke proper hack depending on which OS image we're loading
|
2006-02-24 01:48:57 +03:00
|
|
|
switch (SIM->get_param_enum(BXPN_LOAD32BITOS_WHICH)->get()) {
|
2001-04-10 05:04:59 +04:00
|
|
|
case Load32bitOSLinux:
|
|
|
|
bx_load_linux_hack();
|
|
|
|
break;
|
|
|
|
case Load32bitOSNullKernel:
|
|
|
|
bx_load_null_kernel_hack();
|
|
|
|
break;
|
|
|
|
default:
|
2001-05-30 22:56:02 +04:00
|
|
|
BX_PANIC(("load32bitOSImage: OS not recognized"));
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct gdt_entry
|
|
|
|
{
|
|
|
|
Bit32u low;
|
|
|
|
Bit32u high;
|
|
|
|
};
|
2006-03-07 01:03:16 +03:00
|
|
|
|
2001-04-10 05:04:59 +04:00
|
|
|
struct linux_setup_params
|
|
|
|
{
|
|
|
|
/* 0x000 */ Bit8u orig_x;
|
|
|
|
/* 0x001 */ Bit8u orig_y;
|
|
|
|
/* 0x002 */ Bit16u memory_size_std;
|
|
|
|
/* 0x004 */ Bit16u orig_video_page;
|
|
|
|
/* 0x006 */ Bit8u orig_video_mode;
|
|
|
|
/* 0x007 */ Bit8u orig_video_cols;
|
|
|
|
/* 0x008 */ Bit16u unused1;
|
|
|
|
/* 0x00a */ Bit16u orig_video_ega_bx;
|
|
|
|
/* 0x00c */ Bit16u unused2;
|
|
|
|
/* 0x00e */ Bit8u orig_video_lines;
|
|
|
|
/* 0x00f */ Bit8u orig_video_isVGA;
|
|
|
|
/* 0x010 */ Bit16u orig_video_points;
|
|
|
|
/* 0x012 */ Bit8u pad1[0x40 - 0x12];
|
|
|
|
/* 0x040 */ Bit8u apm_info[0x80 - 0x40];
|
|
|
|
/* 0x080 */ Bit8u hd0_info[16];
|
|
|
|
/* 0x090 */ Bit8u hd1_info[16];
|
|
|
|
/* 0x0a0 */ Bit8u pad2[0x1e0 - 0xa0];
|
|
|
|
/* 0x1e0 */ Bit32u memory_size_ext;
|
|
|
|
/* 0x1e4 */ Bit8u pad3[0x1f1 - 0x1e4];
|
|
|
|
/* 0x1f1 */ Bit8u setup_sects;
|
|
|
|
/* 0x1f2 */ Bit16u mount_root_rdonly;
|
|
|
|
/* 0x1f4 */ Bit16u sys_size;
|
|
|
|
/* 0x1f6 */ Bit16u swap_dev;
|
|
|
|
/* 0x1f8 */ Bit16u ramdisk_flags;
|
|
|
|
/* 0x1fa */ Bit16u vga_mode;
|
|
|
|
/* 0x1fc */ Bit16u orig_root_dev;
|
|
|
|
/* 0x1fe */ Bit16u bootsect_magic;
|
|
|
|
/* 0x200 */ Bit8u pad4[0x210 - 0x200];
|
|
|
|
/* 0x210 */ Bit32u loader_type;
|
|
|
|
/* 0x214 */ Bit32u kernel_start;
|
|
|
|
/* 0x218 */ Bit32u initrd_start;
|
|
|
|
/* 0x21c */ Bit32u initrd_size;
|
|
|
|
/* 0x220 */ Bit8u pad5[0x400 - 0x220];
|
|
|
|
/* 0x400 */ struct gdt_entry gdt[128];
|
|
|
|
/* 0x800 */ Bit8u commandline[2048];
|
|
|
|
};
|
|
|
|
|
2006-03-07 01:03:16 +03:00
|
|
|
static void bx_load_linux_setup_params(Bit32u initrd_start, Bit32u initrd_size)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
|
|
|
struct linux_setup_params *params =
|
2008-04-30 02:14:23 +04:00
|
|
|
(struct linux_setup_params *) BX_MEM(0)->get_vector(0x00090000);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2008-02-15 22:03:54 +03:00
|
|
|
memset(params, '\0', sizeof(*params));
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
/* Video settings (standard VGA) */
|
|
|
|
params->orig_x = 0;
|
|
|
|
params->orig_y = 0;
|
|
|
|
params->orig_video_page = 0;
|
|
|
|
params->orig_video_mode = 3;
|
|
|
|
params->orig_video_cols = 80;
|
|
|
|
params->orig_video_lines = 25;
|
|
|
|
params->orig_video_points = 16;
|
|
|
|
params->orig_video_isVGA = 1;
|
|
|
|
params->orig_video_ega_bx = 3;
|
|
|
|
|
|
|
|
/* Memory size (total mem - 1MB, in KB) */
|
2008-04-30 02:14:23 +04:00
|
|
|
params->memory_size_ext = (BX_MEM(0)->len/(1024*1024) - 1) * 1024;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
/* Boot parameters */
|
|
|
|
params->loader_type = 1;
|
|
|
|
params->bootsect_magic = 0xaa55;
|
|
|
|
params->mount_root_rdonly = 0;
|
|
|
|
params->orig_root_dev = 0x0100;
|
|
|
|
params->initrd_start = initrd_start;
|
|
|
|
params->initrd_size = initrd_size;
|
|
|
|
|
|
|
|
/* Initial GDT */
|
|
|
|
params->gdt[2].high = 0x00cf9a00;
|
|
|
|
params->gdt[2].low = 0x0000ffff;
|
|
|
|
params->gdt[3].high = 0x00cf9200;
|
|
|
|
params->gdt[3].low = 0x0000ffff;
|
|
|
|
}
|
|
|
|
|
2006-03-07 01:03:16 +03:00
|
|
|
void bx_load_linux_hack(void)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
|
|
|
Bit32u initrd_start = 0, initrd_size = 0;
|
|
|
|
|
|
|
|
// The RESET function will have been called first.
|
|
|
|
// Set CPU and memory features which are assumed at this point.
|
|
|
|
|
|
|
|
// Load Linux kernel image
|
2006-02-24 01:48:57 +03:00
|
|
|
bx_load_kernel_image(SIM->get_param_string(BXPN_LOAD32BITOS_PATH)->getptr(), 0x100000);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
// Load initial ramdisk image if requested
|
2008-02-15 22:03:54 +03:00
|
|
|
char *tmpPtr = SIM->get_param_string(BXPN_LOAD32BITOS_INITRD)->getptr();
|
|
|
|
if (tmpPtr && tmpPtr[0]) /* The initial value is "" and not NULL */
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
|
|
|
initrd_start = 0x00800000; /* FIXME: load at top of memory */
|
2008-02-15 22:03:54 +03:00
|
|
|
initrd_size = bx_load_kernel_image(tmpPtr, initrd_start);
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup Linux startup parameters buffer
|
2008-02-15 22:03:54 +03:00
|
|
|
bx_load_linux_setup_params(initrd_start, initrd_size);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
// Enable A20 line
|
2008-02-15 22:03:54 +03:00
|
|
|
BX_SET_ENABLE_A20(1);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
// Setup PICs the way Linux likes it
|
2008-02-15 22:03:54 +03:00
|
|
|
BX_OUTP(0x20, 0x11, 1);
|
|
|
|
BX_OUTP(0xA0, 0x11, 1);
|
|
|
|
BX_OUTP(0x21, 0x20, 1);
|
|
|
|
BX_OUTP(0xA1, 0x28, 1);
|
|
|
|
BX_OUTP(0x21, 0x04, 1);
|
|
|
|
BX_OUTP(0xA1, 0x02, 1);
|
|
|
|
BX_OUTP(0x21, 0x01, 1);
|
|
|
|
BX_OUTP(0xA1, 0x01, 1);
|
|
|
|
BX_OUTP(0x21, 0xFF, 1);
|
|
|
|
BX_OUTP(0xA1, 0xFB, 1);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
// Disable interrupts and NMIs
|
2008-02-15 22:03:54 +03:00
|
|
|
BX_CPU(0)->clear_IF();
|
|
|
|
BX_OUTP(0x70, 0x80, 1);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
// Enter protected mode
|
2003-08-08 04:05:53 +04:00
|
|
|
// Fixed by george (kyriazis at nvidia.com)
|
|
|
|
// BX_CPU(0)->cr0.pe = 1;
|
|
|
|
// BX_CPU(0)->cr0.val32 |= 0x01;
|
|
|
|
|
|
|
|
BX_CPU(0)->SetCR0(BX_CPU(0)->cr0.val32 | 0x01);
|
2008-02-06 01:57:43 +03:00
|
|
|
|
2003-08-08 04:05:53 +04:00
|
|
|
// load esi with real_mode
|
2008-02-06 01:57:43 +03:00
|
|
|
BX_CPU(0)->gen_reg[BX_32BIT_REG_ESI].dword.erx = 0x90000;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
// Set up initial GDT
|
2001-05-23 12:16:07 +04:00
|
|
|
BX_CPU(0)->gdtr.limit = 0x400;
|
|
|
|
BX_CPU(0)->gdtr.base = 0x00090400;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
// Jump to protected mode entry point
|
2008-02-15 22:03:54 +03:00
|
|
|
BX_CPU(0)->jump_protected(NULL, 0x10, 0x100000);
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2006-03-07 01:03:16 +03:00
|
|
|
void bx_load_null_kernel_hack(void)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
|
|
|
// The RESET function will have been called first.
|
|
|
|
// Set CPU and memory features which are assumed at this point.
|
|
|
|
|
2006-02-24 01:48:57 +03:00
|
|
|
bx_load_kernel_image(SIM->get_param_string(BXPN_LOAD32BITOS_PATH)->getptr(), 0x100000);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
// EIP deltas
|
2008-02-15 22:03:54 +03:00
|
|
|
BX_CPU(0)->prev_rip = BX_CPU(0)->gen_reg[BX_32BIT_REG_EIP].dword.erx = 0x100000;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
// CS deltas
|
2001-05-23 12:16:07 +04:00
|
|
|
BX_CPU(0)->sregs[BX_SEG_REG_CS].cache.u.segment.base = 0x00000000;
|
|
|
|
BX_CPU(0)->sregs[BX_SEG_REG_CS].cache.u.segment.limit = 0xFFFFF;
|
|
|
|
BX_CPU(0)->sregs[BX_SEG_REG_CS].cache.u.segment.limit_scaled = 0xFFFFFFFF;
|
|
|
|
BX_CPU(0)->sregs[BX_SEG_REG_CS].cache.u.segment.g = 1; // page gran
|
|
|
|
BX_CPU(0)->sregs[BX_SEG_REG_CS].cache.u.segment.d_b = 1; // 32bit
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2006-03-27 22:02:07 +04:00
|
|
|
#if BX_SUPPORT_ICACHE
|
|
|
|
BX_CPU(0)->updateFetchModeMask();
|
|
|
|
#endif
|
|
|
|
|
2001-04-10 05:04:59 +04:00
|
|
|
// DS deltas
|
2001-05-23 12:16:07 +04:00
|
|
|
BX_CPU(0)->sregs[BX_SEG_REG_DS].cache.u.segment.base = 0x00000000;
|
|
|
|
BX_CPU(0)->sregs[BX_SEG_REG_DS].cache.u.segment.limit = 0xFFFFF;
|
|
|
|
BX_CPU(0)->sregs[BX_SEG_REG_DS].cache.u.segment.limit_scaled = 0xFFFFFFFF;
|
|
|
|
BX_CPU(0)->sregs[BX_SEG_REG_DS].cache.u.segment.g = 1; // page gran
|
|
|
|
BX_CPU(0)->sregs[BX_SEG_REG_DS].cache.u.segment.d_b = 1; // 32bit
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
// CR0 deltas
|
2007-07-09 19:16:14 +04:00
|
|
|
BX_CPU(0)->cr0.set_PE(1); // protected mode
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Bit32u
|
|
|
|
bx_load_kernel_image(char *path, Bit32u paddr)
|
|
|
|
{
|
|
|
|
struct stat stat_buf;
|
|
|
|
int fd, ret;
|
|
|
|
unsigned long size, offset;
|
|
|
|
Bit32u page_size;
|
|
|
|
|
|
|
|
// read in ROM BIOS image file
|
|
|
|
fd = open(path, O_RDONLY
|
|
|
|
#ifdef O_BINARY
|
|
|
|
| O_BINARY
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
if (fd < 0) {
|
2008-02-15 22:03:54 +03:00
|
|
|
BX_INFO(("load_kernel_image: couldn't open image file '%s'.", path));
|
2002-04-18 04:22:20 +04:00
|
|
|
BX_EXIT(1);
|
2006-03-07 01:03:16 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
ret = fstat(fd, &stat_buf);
|
|
|
|
if (ret) {
|
2008-02-15 22:03:54 +03:00
|
|
|
BX_INFO(("load_kernel_image: couldn't stat image file '%s'.", path));
|
2002-04-18 04:22:20 +04:00
|
|
|
BX_EXIT(1);
|
2006-03-07 01:03:16 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2008-02-15 22:03:54 +03:00
|
|
|
size = (unsigned long) stat_buf.st_size;
|
2001-04-10 05:04:59 +04:00
|
|
|
page_size = ((Bit32u)size + 0xfff) & ~0xfff;
|
|
|
|
|
2008-04-30 02:14:23 +04:00
|
|
|
if ((paddr + size) > BX_MEM(0)->len) {
|
2008-02-15 22:03:54 +03:00
|
|
|
BX_INFO(("load_kernel_image: address range > physical memsize!"));
|
2002-04-18 04:22:20 +04:00
|
|
|
BX_EXIT(1);
|
2006-03-07 01:03:16 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
while (size > 0) {
|
2008-04-30 02:14:23 +04:00
|
|
|
ret = read(fd, (bx_ptr_t) BX_MEM(0)->get_vector(paddr + offset), size);
|
2001-04-10 05:04:59 +04:00
|
|
|
if (ret <= 0) {
|
2008-02-15 22:03:54 +03:00
|
|
|
BX_INFO(("load_kernel_image: read failed on image"));
|
2002-04-18 04:22:20 +04:00
|
|
|
BX_EXIT(1);
|
2006-03-07 01:03:16 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
size -= ret;
|
|
|
|
offset += ret;
|
2006-03-07 01:03:16 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
close(fd);
|
2006-01-26 01:20:00 +03:00
|
|
|
BX_INFO(("load_kernel_image: '%s', size=%u read into memory at %08x",
|
|
|
|
path, (unsigned) stat_buf.st_size, (unsigned) paddr));
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
return page_size;
|
|
|
|
}
|