wow, det ser rimelig vildt ud, og nok lidt over mit niveau.
min header fil ser sådan her ud
- /* Copyright (c) 1997-2014, FenixOS Developers
- All Rights Reserved.
-
- This file is subject to the terms and conditions defined in
- file 'LICENSE', which is part of this source code package.
- */
-
- /*! \file globals.h This file holds declarations of various global
- variables.
- */
-
- #ifndef GLOBALS_H
- #define GLOBALS_H
-
- # include <stdint.h>
-
- # include "instruction_wrappers.h"
-
- # include <sysdefines.h>
-
- struct Node
- {
- struct Node *theNextNode;
- uint64_t* endOfNode;
- };
-
- /*! Structure which stores a context. */
- struct AMD64_context
- {
- /* The following fields must be the first in the struct. The order of
- fields must not change or the assembly code will break. */
- uint8_t fp_context[512] __attribute__((aligned (16))); // offset 0x0
- uint64_t rax; // offset 0x200
- uint64_t rbx; // offset 0x208
- uint64_t rcx; // offset 0x210
- uint64_t rdx; // offset 0x218
- uint64_t rdi; // offset 0x220
- uint64_t rsi; // offset 0x228
- uint64_t rbp; // offset 0x230
- uint64_t rsp; // offset 0x238
- uint64_t r8; // offset 0x240
- uint64_t r9; // offset 0x248
- uint64_t r10; // offset 0x250
- uint64_t r11; // offset 0x258
- uint64_t r12; // offset 0x260
- uint64_t r13; // offset 0x268
- uint64_t r14; // offset 0x270
- uint64_t r15; // offset 0x278
- uint64_t rip; // offset 0x280 - first instruction
- uint64_t rflags; // offset 0x288 - set to 0x200 when calling process
- /* Additional fields can be added below */
- int interrupt_context; /*!< True, i.e., not zero, iff the context is
- saved in a interrupt handler. */
- };
-
- /*! Each processor has its own structure of this type. It is used
- to store data which is private to each cpu. */
- struct AMD64_kernel_GS_data
- {
- /* Warning: Do not change or add members to this struct. It would break
- the assembly code. */
-
- /*! The currently executing context. */
- struct AMD64_context * volatile active_context; /* offset 0 */
-
- /*! Scratchpad variable used by the context switch routines. */
- uint64_t scratch_pad1; /* offset 8 */
-
- /*! The stack used in system call handlers. */
- uint64_t syscall_stack; /* offset 16 */
- };
-
- /* ELF image structures. The names from the ELF64 specification are used
- and the structs are derived from the ELF64 specification. */
-
- # define EI_MAG0 0 /*!< The first four bytes in an ELF image is
- 0x7f 'E' 'L' 'F'. */
- # define EI_MAG1 1
- # define EI_MAG2 2
- # define EI_MAG3 3
- # define EI_CLASS 4 /*!< The specific ELF image class. */
- # define EI_DATA 5 /*!< Describes if the image is big- or little-
- endian. */
- # define EI_VERSION 6 /*!< The version of the ELF specification the
- image adheres to. */
- # define EI_OSABI 7 /*!< Type of OS the image can be run on. */
- # define EI_ABIVERSION 8 /*!< Version of the ABI used. */
- # define EI_PAD 9 /*!< First unused byte in the identification
- array. */
- # define EI_NIDENT 16 /*!< Number of entries in the identification
- array */
-
- /*! Defines an ELF64 file header. */
- struct Elf64_Ehdr
- {
- uint8_t e_ident[EI_NIDENT]; /*!< Array of bytes that shows that
- this is an ELF image and what type
- of ELF image it is. */
- uint16_t e_type; /*!< The type of ELF executable image. */
- uint16_t e_machine; /*!< Identifies the type of machine
- that the image can execute on. */
- uint32_t e_version; /*!< The version of the ELF
- specification the image adheres to. */
- uint64_t e_entry; /*!< Start address of the executable. */
- uint64_t e_phoff; /*!< The offset into the image where
- the program header table is
- found. */
- uint64_t e_shoff; /*!< The offset into the image where
- the program header table is
- found. */
- uint32_t e_flags; /*!< Flags that are machine specific.
- These can be used to differentiate
- between similar machines. */
- uint16_t e_ehsize; /*!< The size, in bytes, of the header. */
- uint16_t e_phentsize; /*!< The size, in bytes, of each entry
- in the program header table. */
- uint16_t e_phnum; /*!< The size, in entries, of the
- program header table. */
- uint16_t e_shentsize; /*!< The size, in bytes, of each entry
- in the section header table. */
- uint16_t e_shnum; /*!< The size, in entries, of the
- section header table. */
- uint16_t e_shstrndx; /*!< The index, into the section table,
- of the section name string table.*/
- };
-
- /*! Defines an entry in the ELF program header table. Each entry
- corresponds to a segment. */
- struct Elf64_Phdr
- {
- uint32_t p_type; /*!< Segments can have several types. p_type holds the
- type. */
- uint32_t p_flags; /*!< The attribute flags of the segment. */
- uint64_t p_offset; /*!< Offset into the image of the first byte of the
- segment */
- uint64_t p_vaddr; /*!< The (virtual) address to which the segment is to
- be loaded. */
- uint64_t p_paddr; /*!< Not used. */
- uint64_t p_filesz; /*!< The number of bytes the segment occupies in the
- image. */
- uint64_t p_memsz; /*!< The number of bytes the segment occupies in
- memory. */
- uint64_t p_align; /*!< The alignment the segment should have in memory.
- This field is currently being ignored. */
- };
-
- /* Values used in p_type */
- # define PT_NULL 0 /*!< The entry is not used. */
- # define PT_LOAD 1 /*!< The segment can be loaded into memory. */
- # define PT_PHDR 6 /*!< The segment only hold a program header table. */
- /* Values used in p_flags */
- # define PF_X 0x1 /*!< Segment can be executed.*/
- # define PF_W 0x2 /*!< Segment can be written. */
- # define PF_R 0x4 /*!< Segment can be read. */
- # define PF_MASKPERM 0x0000FFFF /*!< Used to mask the permission bits */
-
- /*! Copies an ELF image to memory. Does some checks to avoid that corrupt
- images gets copied to memory.
- \return The address to the first instruction to execute. */
- extern unsigned long
- copy_ELF(const struct Elf64_Ehdr* const elf_image
- /*!< Points to the ELF image to copy. */);
-
- /*! Returns the currently active context. */
- inline struct AMD64_context *
- get_active_context(void)
- {
- register struct AMD64_context * context;
- __asm volatile("mov %%gs:0,%0" : "=r" (context) : : );
- return context;
- }
-
- /*! Sets the currently active context. */
- inline void
- set_active_context(struct AMD64_context * const context /*!< the new context. */)
- {
- __asm volatile("mov %0,%%gs:0" : : "r" (context) : );
- }
-
- /*! Performs a context switch and go to user mode. Will return to caller if
- the context is not valid. */
- inline void
- go_to_user_mode(void)
- {
- /* This is somewhat of a hack. This code will perform a decent context
- switch but more state should be switched. There are also performance
- optimizations to be made. */
- struct AMD64_context * const context = get_active_context();
-
- if (0 == context)
- {
- /* No proper context. */
- return;
- }
-
- /* Set FPU/MMX/SSE context. */
- fxrstor(context->fp_context);
-
- /* swap the gs. */
- __asm volatile("swapgs" :::);
-
- if (context->interrupt_context)
- {
-
- //%rax is system call id
- //%rdi index of ELF-image
- /* Set registers and return via iretq */
- __asm volatile("movq 0x200(%%rcx),%%rax \n \
- movq 0x208(%%rcx),%%rbx \n \
- movq 0x220(%%rcx),%%rdi \n \
- movq 0x228(%%rcx),%%rsi \n \
- movq 0x230(%%rcx),%%rbp \n \
- movq 0x240(%%rcx),%%r8 \n \
- movq 0x248(%%rcx),%%r9 \n \
- movq 0x250(%%rcx),%%r10 \n \
- movq 0x258(%%rcx),%%r11 \n \
- movq 0x260(%%rcx),%%r12 \n \
- movq 0x268(%%rcx),%%r13 \n \
- movq 0x270(%%rcx),%%r14 \n \
- movq 0x278(%%rcx),%%r15 \n \
- movq $11,%%rdx \n \
- pushq %%rdx \n \
- pushq 0x238(%%rcx) \n \
- pushq 0x288(%%rcx) \n \
- pushq $19 \n \
- pushq 0x280(%%rcx) \n \
- pushq 0x210(%%rcx) \n \
- pushq 0x218(%%rcx) \n \
- mov %%dx,%%gs \n \
- mov %%dx,%%fs \n \
- mov %%dx,%%es \n \
- mov %%dx,%%ds \n \
- popq %%rdx \n \
- popq %%rcx \n \
- iretq"
- :
- : "c"(context)
- : "memory");
- }
- else
- {
- /* Set registers and return via sysret */
- __asm volatile("movq 0x200(%%rcx),%%rax \n \
- movq 0x208(%%rcx),%%rbx \n \
- movq 0x218(%%rcx),%%rdx \n \
- movq 0x220(%%rcx),%%rdi \n \
- movq 0x228(%%rcx),%%rsi \n \
- movq 0x230(%%rcx),%%rbp \n \
- movq 0x240(%%rcx),%%r8 \n \
- movq 0x248(%%rcx),%%r9 \n \
- movq 0x250(%%rcx),%%r10 \n \
- movq 0x260(%%rcx),%%r12 \n \
- movq 0x268(%%rcx),%%r13 \n \
- movq 0x270(%%rcx),%%r14 \n \
- movq 0x278(%%rcx),%%r15 \n \
- movq 0x288(%%rcx),%%r11 \n \
- pushq 0x238(%%rcx) \n \
- pushq 0x280(%%rcx) \n \
- mov $11,%%ecx \n \
- mov %%cx,%%gs \n \
- mov %%cx,%%fs \n \
- mov %%cx,%%es \n \
- mov %%cx,%%ds \n \
- popq %%rcx \n \
- popq %%rsp \n \
- sysretq"
- :
- : "c"(context)
- : "memory");
- }
- }
-
- extern void
- amd64_syscall_entry_point(void) __attribute__ ((noreturn));
-
- extern void
- amd64_syscall_dummy_target(void) __attribute__ ((noreturn));
-
- extern void
- amd64_enter_kernel(void) __attribute__ ((noreturn));
-
- extern void
- amd64_handle_interrupt_user(int interrupt) __attribute__ ((noreturn));
-
- extern void
- amd64_handle_interrupt_super(int interrupt);
-
- extern void
- amd64_handle_exception_user(int exc) __attribute__ ((noreturn));
-
- extern void
- amd64_handle_exception_super(int exc);
-
- extern void
- amd64_handle_exception_w_error_user(int exc,
- uint32_t code) __attribute__ ((noreturn));
-
- extern void
- amd64_handle_exception_w_error_super(int exc, uint32_t code);
-
- extern void
- amd64_handle_nmi_user(int dummy) __attribute__ ((noreturn));
-
- extern void
- amd64_handle_nmi_super(int dummy);
-
- extern const struct Elf64_Ehdr* const
- ELF_images[1];
- /*!< Array of pointers to ELF images. The array ends with a null
- pointer. The array itself is created by the linker and no
- definition is visible in the source code. */
-
- /*! Assembly routine which will halt the calling processor forever. */
- extern void
- amd64_halt(void) __attribute__ ((noreturn));
-
- /*! Assembly routine which will cause the calling processor to envoke
- the Bochs debugger. */
- extern void
- amd64_enter_debugger(void);
-
-
- /*! Allocates a memory block.
- \return an address to the memory block or an error code if
- the allocation was not successful. */
- extern long
- kalloc(const register uint64_t length
- /*!< The number of bytes to allocate. */);
-
- /*! Frees a previously allocated a memory block.
- \return ALL_OK if successful or an error code if
- the free was not successful. */
- extern long
- kfree(const register uint64_t address
- /*!< The address to the memory block to free. */);
-
- /*! Outputs a string through the terminal emulator. */
- extern void
- kprints(const char* const string
- /*!< Points to a null terminated string */);
-
- /*! Outputs an unsigned 64-bit value through the terminal emulator. */
- extern void
- kprinthex(const register uint64_t value /*! value to be printed. */);
-
- extern uint64_t allign32( const register uint64_t length);
-
- #endif
og er en del at det større system, så det er ikke sikker det giver mening.
Mvh