/*
 * segments.h
 *
 * Copyright (C) 2013 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _SEGMENTS_H_
#define _SEGMENTS_H_

#include <common.h>

#define GDT_MAX_ENTRIES 8

#pragma pack(push, 1)
typedef struct
{
    dword_t limit : 16;
    dword_t base : 24;
    dword_t accessed : 1;
    dword_t readwrite : 1;
    dword_t dirconf : 1;
    dword_t executable : 1;
    dword_t standard : 1;
    dword_t rpl : 2;
    dword_t present : 1;
    dword_t limit_high : 4;
    dword_t always_zero : 2;
    dword_t size : 1;
    dword_t granularity : 1;
    dword_t base_high : 8;
} gdt_descriptor_t;
#pragma pack(pop)

typedef struct
{
    dword_t link;
    dword_t esp0;
    dword_t ss0;
    dword_t esp1;
    dword_t ss1;
    dword_t esp2;
    dword_t ss2;
    dword_t cr3;
    dword_t eip;
    dword_t eflags;
    dword_t eax;
    dword_t ecx;
    dword_t edx;
    dword_t ebx;
    dword_t esp;
    dword_t ebp;
    dword_t esi;
    dword_t edi;
    dword_t es;
    dword_t cs;
    dword_t ss;
    dword_t ds;
    dword_t fs;
    dword_t gs;
    dword_t ldtr;
    dword_t iopb;
} tss_entry_t;

void segments_init(void);
dword_t get_kernel_esp(void);
void set_kernel_esp(dword_t esp);
word_t get_kernel_code_selector(void);
word_t get_kernel_data_selector(void);
word_t get_user_code_selector(void);
word_t get_user_data_selector(void);
word_t gdt_create_segment(dword_t base, dword_t limit, bool_t executable, byte_t priv_level, bool_t readwrite, bool_t dirconf, bool_t large);
word_t gdt_create_tss(tss_entry_t *base, dword_t limit);

#endif
