199767f8919635c4928607450d9e0abb932109ceToomas Soome * This file and its contents are supplied under the terms of the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Common Development and Distribution License ("CDDL"), version 1.0.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * You may only use this file in accordance with the terms of version
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1.0 of the CDDL.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * A full copy of the text of the CDDL should have accompanied this
199767f8919635c4928607450d9e0abb932109ceToomas Soome * source. A copy of the CDDL is also available via the Internet at
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright 2016 Toomas Soome <tsoome@me.com>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Relocate is needed to support loading code which has to be located
199767f8919635c4928607450d9e0abb932109ceToomas Soome * below 1MB, as both BTX and loader are using low memory area.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Relocate and start loaded code. Since loaded code may need to be
199767f8919635c4928607450d9e0abb932109ceToomas Soome * placed in an already occupied memory area, the code is moved to a safe
199767f8919635c4928607450d9e0abb932109ceToomas Soome * memory area and then btx __exec will be called with physical pointer
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to this area. __exec will set the pointer to %eax and call *%eax,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * so that on entry, we have the new "base" address in %eax.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Relocate will first set up and load new safe GDT to shut down BTX,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * then loaded code will be relocated to final memory location,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * then machine will be switched from 32-bit protected mode to 16-bit
199767f8919635c4928607450d9e0abb932109ceToomas Soome * protected mode following by switch to real mode with A20 enabled or
199767f8919635c4928607450d9e0abb932109ceToomas Soome * disabled. Finally the loaded code will be started and it will take
199767f8919635c4928607450d9e0abb932109ceToomas Soome * over the whole system.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * For now, the known "safe" memory area for relocate is 0x600,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the actual "free" memory is supposed to start from 0x500, leaving
199767f8919635c4928607450d9e0abb932109ceToomas Soome * first 0x100 bytes in reserve. As relocate code+data is very small,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * it will leave enough space to set up boot blocks to 0:7c00 or load
199767f8919635c4928607450d9e0abb932109ceToomas Soome * linux kernel below 1MB space.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * segment selectors
199767f8919635c4928607450d9e0abb932109ceToomas Soome * set up GDT from new location
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* set up jump */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* set up gdt */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* load gdt */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* update cs */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * relocate data loop. load source, dest and size from
199767f8919635c4928607450d9e0abb932109ceToomas Soome * relocater_data[i], 0 value will stop the loop.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * registers used for move: %esi, %edi, %ecx.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * %ebx to keep base
199767f8919635c4928607450d9e0abb932109ceToomas Soome * %edx for relocater_data offset
199767f8919635c4928607450d9e0abb932109ceToomas Soome movl (relocater_data-relocater)(%ebx, %edx, 4), %eax
199767f8919635c4928607450d9e0abb932109ceToomas Soome movl (relocater_data-relocater)(%ebx, %edx, 4), %esi
199767f8919635c4928607450d9e0abb932109ceToomas Soome movl (relocater_data-relocater)(%ebx, %edx, 4), %edi
199767f8919635c4928607450d9e0abb932109ceToomas Soome movl (relocater_data-relocater)(%ebx, %edx, 4), %ecx
199767f8919635c4928607450d9e0abb932109ceToomas Soome * data is relocated, switch to 16-bit mode
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* clear PE */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * set real mode irq offsets
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Should A20 be left enabled?
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* movw imm16, %ax */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Disable A20 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * set up registers
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* movw imm16, %ax. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* movw imm16, %ax. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* movw imm16, %ax. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* movw imm16, %ax. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* movw imm16, %ax. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* movw imm16, %ax. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* movw imm32, %eax. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* movw imm32, %edx. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* movw imm32, %ebx. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* movw imm32, %eax. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* movw imm32, %ebp. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* GDT to reset BTX */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* reserve space for 3 entries */
199767f8919635c4928607450d9e0abb932109ceToomas Soome .long 0 /* src */
199767f8919635c4928607450d9e0abb932109ceToomas Soome .long 0 /* dest */
199767f8919635c4928607450d9e0abb932109ceToomas Soome .long 0 /* size */
199767f8919635c4928607450d9e0abb932109ceToomas Soome .long 0 /* src */
199767f8919635c4928607450d9e0abb932109ceToomas Soome .long 0 /* dest */
199767f8919635c4928607450d9e0abb932109ceToomas Soome .long 0 /* size */
199767f8919635c4928607450d9e0abb932109ceToomas Soome .long 0 /* src */
199767f8919635c4928607450d9e0abb932109ceToomas Soome .long 0 /* dest */
199767f8919635c4928607450d9e0abb932109ceToomas Soome .long 0 /* size */