;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                                                              ;;
;; Copyright (C) KolibriOS team 2004-2012. All rights reserved. ;;
;; Distributed under terms of the GNU General Public License    ;;
;;                                                              ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

$Revision$


dpl0    equ  10010000b     ; data read       dpl0
drw0    equ  10010010b      ; data read/write dpl0
drw3    equ  11110010b      ; data read/write dpl3
cpl0    equ  10011010b      ; code read dpl0
cpl3    equ  11111010b      ; code read dpl3

D32     equ  01000000b      ; 32bit segment
G32     equ  10000000b      ; page gran


;;;;;;;;;;;;cpu_caps flags;;;;;;;;;;;;;;;;

CPU_386        equ 3
CPU_486        equ 4
CPU_PENTIUM    equ 5
CPU_P6         equ 6
CPU_PENTIUM4   equ 0x0F

CAPS_FPU       equ    00 ;on-chip x87 floating point unit
CAPS_VME       equ    01 ;virtual-mode enhancements
CAPS_DE        equ    02 ;debugging extensions
CAPS_PSE       equ    03 ;page-size extensions
CAPS_TSC       equ    04 ;time stamp counter
CAPS_MSR       equ    05 ;model-specific registers
CAPS_PAE       equ    06 ;physical-address extensions
CAPS_MCE       equ    07 ;machine check exception
CAPS_CX8       equ    08 ;CMPXCHG8B instruction
CAPS_APIC      equ    09 ;on-chip advanced programmable
                         ; interrupt controller
;                     10 ;unused
CAPS_SEP       equ    11 ;SYSENTER and SYSEXIT instructions
CAPS_MTRR      equ    12 ;memory-type range registers
CAPS_PGE       equ    13 ;page global extension
CAPS_MCA       equ    14 ;machine check architecture
CAPS_CMOV      equ    15 ;conditional move instructions
CAPS_PAT       equ    16 ;page attribute table

CAPS_PSE36     equ    17 ;page-size extensions
CAPS_PSN       equ    18 ;processor serial number
CAPS_CLFLUSH   equ    19 ;CLFUSH instruction

CAPS_DS        equ    21 ;debug store
CAPS_ACPI      equ    22 ;thermal monitor and software
                         ;controlled clock supported
CAPS_MMX       equ    23 ;MMX instructions
CAPS_FXSR      equ    24 ;FXSAVE and FXRSTOR instructions
CAPS_SSE       equ    25 ;SSE instructions
CAPS_SSE2      equ    26 ;SSE2 instructions
CAPS_SS        equ    27 ;self-snoop
CAPS_HTT       equ    28 ;hyper-threading technology
CAPS_TM        equ    29 ;thermal monitor supported
CAPS_IA64      equ    30 ;IA64 capabilities
CAPS_PBE       equ    31 ;pending break enable

;ecx
CAPS_SSE3      equ    32 ;SSE3 instructions
;                     33
;                     34
CAPS_MONITOR   equ    35 ;MONITOR/MWAIT instructions
CAPS_DS_CPL    equ    36 ;
CAPS_VMX       equ    37 ;virtual mode extensions
;                     38 ;
CAPS_EST       equ    39 ;enhansed speed step
CAPS_TM2       equ    40 ;thermal monitor2 supported
;                     41
CAPS_CID       equ    42 ;
;                     43
;                     44
CAPS_CX16      equ    45 ;CMPXCHG16B instruction
CAPS_xTPR      equ    46 ;
;
;reserved
;
;ext edx /ecx
CAPS_SYSCAL    equ    64 ;
CAPS_XD        equ    65 ;execution disable
CAPS_FFXSR     equ    66 ;
CAPS_RDTSCP    equ    67 ;
CAPS_X64       equ    68 ;
CAPS_3DNOW     equ    69 ;
CAPS_3DNOWEXT  equ    70 ;
CAPS_LAHF      equ    71 ;
CAPS_CMP_LEG   equ    72 ;
CAPS_SVM       equ    73 ;secure virual machine
CAPS_ALTMOVCR8 equ    74 ;

; CPU MSR names
MSR_SYSENTER_CS         equ     0x174
MSR_SYSENTER_ESP        equ     0x175
MSR_SYSENTER_EIP        equ     0x176
MSR_AMD_EFER            equ     0xC0000080      ; Extended Feature Enable Register
MSR_AMD_STAR            equ     0xC0000081      ; SYSCALL/SYSRET Target Address Register

CR0_PE         equ    0x00000001   ;protected mode
CR0_MP         equ    0x00000002   ;monitor fpu
CR0_EM         equ    0x00000004   ;fpu emulation
CR0_TS         equ    0x00000008   ;task switch
CR0_ET         equ    0x00000010   ;extension type hardcoded to 1
CR0_NE         equ    0x00000020   ;numeric error
CR0_WP         equ    0x00010000   ;write protect
CR0_AM         equ    0x00040000   ;alignment check
CR0_NW         equ    0x20000000   ;not write-through
CR0_CD         equ    0x40000000   ;cache disable
CR0_PG         equ    0x80000000   ;paging


CR4_VME        equ    0x0001
CR4_PVI        equ    0x0002
CR4_TSD        equ    0x0004
CR4_DE         equ    0x0008
CR4_PSE        equ    0x0010
CR4_PAE        equ    0x0020
CR4_MCE        equ    0x0040
CR4_PGE        equ    0x0080
CR4_PCE        equ    0x0100
CR4_OSFXSR     equ    0x0200
CR4_OSXMMEXPT  equ    0x0400

SSE_IE         equ    0x0001
SSE_DE         equ    0x0002
SSE_ZE         equ    0x0004
SSE_OE         equ    0x0008
SSE_UE         equ    0x0010
SSE_PE         equ    0x0020
SSE_DAZ        equ    0x0040
SSE_IM         equ    0x0080
SSE_DM         equ    0x0100
SSE_ZM         equ    0x0200
SSE_OM         equ    0x0400
SSE_UM         equ    0x0800
SSE_PM         equ    0x1000
SSE_FZ         equ    0x8000

SSE_INIT equ (SSE_IM+SSE_DM+SSE_ZM+SSE_OM+SSE_UM+SSE_PM)

IRQ_PIC        equ    0
IRQ_APIC       equ    1

struct  TSS
        _back       rw 2
        _esp0       rd 1
        _ss0        rw 2
        _esp1       rd 1
        _ss1        rw 2
        _esp2       rd 1
        _ss2        rw 2
        _cr3        rd 1
        _eip        rd 1
        _eflags     rd 1
        _eax        rd 1
        _ecx        rd 1
        _edx        rd 1
        _ebx        rd 1
        _esp        rd 1
        _ebp        rd 1
        _esi        rd 1
        _edi        rd 1
        _es         rw 2
        _cs         rw 2
        _ss         rw 2
        _ds         rw 2
        _fs         rw 2
        _gs         rw 2
        _ldt        rw 2
        _trap       rw 1
        _io         rw 1
                    rb 24
        _io_map_0   rb 4096
        _io_map_1   rb 4096
ends

PARTITION_COUNT     equ 64
DRIVE_DATA_SIZE     equ (16+PARTITION_COUNT*100)

OS_BASE             equ 0x80000000

window_data         equ (OS_BASE+0x0001000)

CURRENT_TASK        equ (OS_BASE+0x0003000)
TASK_COUNT          equ (OS_BASE+0x0003004)
TASK_BASE           equ (OS_BASE+0x0003010)
TASK_DATA           equ (OS_BASE+0x0003020)
TASK_EVENT          equ (OS_BASE+0x0003020)

CDDataBuf           equ (OS_BASE+0x0005000)

;unused                 0x6000 - 0x8fff

BOOT_VARS           equ (OS_BASE)               ;0x9000

idts                equ (OS_BASE+0x000B100)
WIN_STACK           equ (OS_BASE+0x000C000)
WIN_POS             equ (OS_BASE+0x000C400)
FDD_BUFF            equ (OS_BASE+0x000D000)     ;512

WIN_TEMP_XY         equ (OS_BASE+0x000F300)
KEY_COUNT           equ (OS_BASE+0x000F400)
KEY_BUFF            equ (OS_BASE+0x000F401)

BTN_COUNT           equ (OS_BASE+0x000F500)
BTN_BUFF            equ (OS_BASE+0x000F501)


BTN_ADDR            equ (OS_BASE+0x000FE88)
MEM_AMOUNT          equ (OS_BASE+0x000FE8C)

SYS_SHUTDOWN        equ (OS_BASE+0x000FF00)
TASK_ACTIVATE       equ (OS_BASE+0x000FF01)


TMP_STACK_TOP       equ 0x006CC00

sys_pgdir           equ (OS_BASE+0x006F000)

SLOT_BASE           equ (OS_BASE+0x0080000)

VGABasePtr          equ (OS_BASE+0x00A0000)

CLEAN_ZONE          equ (_CLEAN_ZONE-OS_BASE)
IDE_DMA             equ (_IDE_DMA-OS_BASE)

; unused?
SB16Buffer          equ (OS_BASE+0x02A0000)
SB16_Status         equ (OS_BASE+0x02B0000)

UPPER_KERNEL_PAGES  equ (OS_BASE+0x0400000)

virtual at              (OS_BASE+0x05FFF80)
  tss  TSS
end virtual

HEAP_BASE           equ (OS_BASE+0x0800000)
HEAP_MIN_SIZE       equ 0x01000000

page_tabs           equ 0xFDC00000
app_page_tabs       equ 0xFDC00000
kernel_tabs         equ (page_tabs+ (OS_BASE shr 10))   ;0xFDE00000
master_tab          equ (page_tabs+ (page_tabs shr 10)) ;0xFDFF70000

LFB_BASE            equ 0xFE000000


new_app_base        equ 0;

twdw                equ 0x2000   ;(CURRENT_TASK-window_data)

std_application_base_address   equ new_app_base
RING0_STACK_SIZE    equ (0x2000 - 512)    ;512 байт для контекста FPU

REG_SS              equ (RING0_STACK_SIZE-4)
REG_APP_ESP         equ (RING0_STACK_SIZE-8)
REG_EFLAGS          equ (RING0_STACK_SIZE-12)
REG_CS              equ (RING0_STACK_SIZE-16)
REG_EIP             equ (RING0_STACK_SIZE-20)
REG_EAX             equ (RING0_STACK_SIZE-24)
REG_ECX             equ (RING0_STACK_SIZE-28)
REG_EDX             equ (RING0_STACK_SIZE-32)
REG_EBX             equ (RING0_STACK_SIZE-36)
REG_ESP             equ (RING0_STACK_SIZE-40)  ;RING0_STACK_SIZE-20
REG_EBP             equ (RING0_STACK_SIZE-44)
REG_ESI             equ (RING0_STACK_SIZE-48)
REG_EDI             equ (RING0_STACK_SIZE-52)
REG_RET             equ (RING0_STACK_SIZE-56)  ;irq0.return


PG_UNMAP            equ 0x000
PG_MAP              equ 0x001
PG_WRITE            equ 0x002
PG_SW               equ 0x003
PG_USER             equ 0x005
PG_UW               equ 0x007
PG_NOCACHE          equ 0x018
PG_LARGE            equ 0x080
PG_GLOBAL           equ 0x100

PG_SHARED           equ 0x200

;;;;;;;;;;;boot time variables

BOOT_BPP            equ 0x9000    ;byte   bits per pixel
BOOT_PITCH          equ 0x9001    ;word   scanline length
BOOT_VESA_MODE      equ 0x9008    ;word   vesa video mode
BOOT_X_RES          equ 0x900A    ;word   X res
BOOT_Y_RES          equ 0x900C    ;word   Y res
BOOT_BANK_SW        equ 0x9014    ;dword  Vesa 1.2 pm bank switch
BOOT_LFB            equ 0x9018    ;dword  Vesa 2.0 LFB address
BOOT_MTRR           equ 0x901C    ;byte   0 or 1 : enable MTRR graphics acceleration
;BOOT_LOG            equ 0x901D    ;byte   not used anymore (0 or 1 : enable system log display)
BOOT_LAUNCHER_START equ 0x901D    ;byte  (0 or 1) start the first app (right now it's LAUNCHER) after kernel is loaded?
;BOOT_DIRECT_LFB     equ 0x901E    ;byte   0 or 1 : enable direct lfb write, paging disabled
BOOT_DEBUG_PRINT    equ 0x901E    ;byte   If nonzero, duplicates debug output to the screen.
BOOT_DMA            equ 0x901F    ;
BOOT_PCI_DATA       equ 0x9020    ;8bytes pci data
BOOT_VRR            equ 0x9030    ;byte   VRR start enabled 1, 2-no
BOOT_IDE_BASE_ADDR  equ 0x9031   ;word   IDEContrRegsBaseAddr
BOOT_MEM_AMOUNT     equ 0x9034    ;dword  memory amount

BOOT_APM_ENTRY      equ 0x9040
BOOT_APM_VERSION    equ 0x9044
BOOT_APM_FLAGS      equ 0x9046    ;unused
BOOT_APM_CODE_32    equ 0x9050
BOOT_APM_CODE_16    equ 0x9052
BOOT_APM_DATA_16    equ 0x9054
BOOT_IDE_BAR0_16    equ 0x9056
BOOT_IDE_BAR1_16    equ 0x9058
BOOT_IDE_BAR2_16    equ 0x905A
BOOT_IDE_BAR3_16    equ 0x905C
BOOT_IDE_PI_16      equ 0x905E
BOOT_IDE_INTERR_16  equ 0x9060

TMP_FILE_NAME       equ     0
TMP_CMD_LINE        equ  1024
TMP_ICON_OFFS       equ  1280


EVENT_REDRAW       equ 0x00000001
EVENT_KEY          equ 0x00000002
EVENT_BUTTON       equ 0x00000004
EVENT_BACKGROUND   equ 0x00000010
EVENT_MOUSE        equ 0x00000020
EVENT_IPC          equ 0x00000040
EVENT_NETWORK      equ 0x00000080
EVENT_DEBUG        equ 0x00000100
EVENT_NETWORK2     equ 0x00000200
EVENT_EXTENDED     equ 0x00000400

EV_INTR            equ 1

struct  THR_DATA
                        rb (8192-512)
;       pl0_stack
        fpu_state       rb 512
        tls_page        rb 4096
        pdbr            rb 4096
ends

virtual at (OS_BASE-sizeof.THR_DATA)
  thr_data  THR_DATA
end virtual

struct  SYS_VARS
        bpp             dd ?
        scanline        dd ?
        vesa_mode       dd ?
        x_res           dd ?
        y_res           dd ?
ends

struct  APPOBJ                  ; common object header
        magic           dd ?    ;
        destroy         dd ?    ; internal destructor
        fd              dd ?    ; next object in list
        bk              dd ?    ; prev object in list
        pid             dd ?    ; owner id
ends

APP_OBJ_OFFSET  equ 48
APP_EV_OFFSET   equ 40

struct  CURSOR          APPOBJ
        base            dd ?   ;allocated memory
        hot_x           dd ?   ;hotspot coords
        hot_y           dd ?

        list_next       dd ?   ;next cursor in cursor list
        list_prev       dd ?   ;prev cursor in cursor list
        dev_obj         dd ?   ;device depended data
ends


struct  EVENT           APPOBJ
        id              dd ?   ;event uid
        state           dd ?   ;internal flags
        code            dd ?
                        rd 5
ends


struct  SMEM
        bk              dd ?
        fd              dd ?    ;+4
        base            dd ?    ;+8
        size            dd ?    ;+12
        access          dd ?    ;+16
        refcount        dd ?    ;+20
        name            rb 32   ;+24
ends

struct  SMAP            APPOBJ
        base            dd ?   ;mapped base
        parent          dd ?   ;SMEM
ends

struct  DLLDESCR
        bk              dd ?
        fd              dd ?    ;+4
        data            dd ?    ;+8
        size            dd ?    ;+12
        timestamp       dq ?
        refcount        dd ?
        defaultbase     dd ?
        coff_hdr        dd ?
        symbols_ptr     dd ?
        symbols_num     dd ?
        symbols_lim     dd ?
        exports         dd ?   ;export table
        name            rb 260
ends

struct  HDLL
        fd              dd ?   ;next object in list
        bk              dd ?   ;prev object in list
        pid             dd ?   ;owner id

        base            dd ?   ;mapped base
        size            dd ?   ;mapped size
        refcount        dd ?   ;reference counter for this process and this lib
        parent          dd ?   ;DLLDESCR
ends

struct  display_t
        x               dd ?
        y               dd ?
        width           dd ?
        height          dd ?
        bpp             dd ?
        vrefresh        dd ?
        pitch           dd ?
        lfb             dd ?

        modes           dd ?
        ddev            dd ?
        connector       dd ?
        crtc            dd ?

        cr_list.next    dd ?
        cr_list.prev    dd ?

        cursor          dd ?

        init_cursor     dd ?
        select_cursor   dd ?
        show_cursor     dd ?
        move_cursor     dd ?
        restore_cursor  dd ?
        disable_mouse   dd ?
        mask_seqno      dd ?
        check_mouse     dd ?
        check_m_pixel   dd ?
ends

struct  BOOT_DATA
        bpp             dd ?
        scanline        dd ?
        vesa_mode       dd ?
        x_res           dd ?
        y_res           dd ?
        mouse_port      dd ?
        bank_switch     dd ?
        lfb             dd ?
        vesa_mem        dd ?
        log             dd ?
        direct_lfb      dd ?
        pci_data        dd ?
                        dd ?
        vrr             dd ?
        ide_base        dd ?
        mem_amount      dd ?
        pages_count     dd ?
        pagemap_size    dd ?
        kernel_max      dd ?
        kernel_pages    dd ?
        kernel_tables   dd ?

        cpu_vendor      dd ?
                        dd ?
                        dd ?
        cpu_sign        dd ?
        cpu_info        dd ?
        cpu_caps        dd ?
                        dd ?
                        dd ?
ends

struct  LHEAD
        next            dd ?   ;next object in list
        prev            dd ?   ;prev object in list
ends

struct  MUTEX
        lhead   LHEAD
        count   dd ?
ends

struct  PCIDEV
        bk              dd ?
        fd              dd ?
        vendor_device_id dd ?
        class           dd ?
        devfn           db ?
        bus             db ?
ends

; The following macro assume that we are on uniprocessor machine.
; Serious work is needed for multiprocessor machines.
macro spin_lock_irqsave spinlock
{
        pushf
        cli
}
macro spin_unlock_irqrestore spinlock
{
        popf
}
macro spin_lock_irq spinlock
{
        cli
}
macro spin_unlock_irq spinlock
{
        sti
}

struct  MEM_STATE
        mutex           MUTEX
        smallmap        dd ?
        treemap         dd ?
        topsize         dd ?
        top             dd ?
        smallbins       rd 4*32
        treebins        rd 32
ends

struct  PG_DATA
        mem_amount      dd ?
        vesa_mem        dd ?
        pages_count     dd ?
        pages_free      dd ?
        pages_faults    dd ?
        pagemap_size    dd ?
        kernel_pages    dd ?
        kernel_tables   dd ?
        sys_page_dir    dd ?
        mutex           MUTEX
ends

struct  SRV
        srv_name        rb 16    ;ASCIIZ string
        magic           dd ?     ;+0x10 ;'SRV '
        size            dd ?     ;+0x14 ;size of structure SRV
        fd              dd ?     ;+0x18 ;next SRV descriptor
        bk              dd ?     ;+0x1C ;prev SRV descriptor
        base            dd ?     ;+0x20 ;service base address
        entry           dd ?     ;+0x24 ;service START function
        srv_proc        dd ?     ;+0x28 ;user mode service handler
        srv_proc_ex     dd ?     ;+0x2C ;kernel mode service handler
ends

struct USBSRV
        srv             SRV
        usb_func        dd ?
ends

struct USBFUNC
        strucsize       dd ?
        add_device      dd ?
        device_disconnect dd ?
ends

DRV_ENTRY    equ  1
DRV_EXIT     equ -1

struct  COFF_HEADER
        machine         dw ?
        nSections       dw ?
        DataTime        dd ?
        pSymTable       dd ?
        nSymbols        dd ?
        optHeader       dw ?
        flags           dw ?
ends

struct  COFF_SECTION
        Name            rb 8
        VirtualSize     dd ?
        VirtualAddress  dd ?
        SizeOfRawData   dd ?
        PtrRawData      dd ?
        PtrReloc        dd ?
        PtrLinenumbers  dd ?
        NumReloc        dw ?
        NumLinenum      dw ?
        Characteristics dd ?
ends

struct  COFF_RELOC
        VirtualAddress  dd ?
        SymIndex        dd ?
        Type            dw ?
ends

struct  COFF_SYM
        Name            rb 8
        Value           dd ?
        SectionNumber   dw ?
        Type            dw ?
        StorageClass    db ?
        NumAuxSymbols   db ?
ends

struct  IOCTL
        handle          dd ?
        io_code         dd ?
        input           dd ?
        inp_size        dd ?
        output          dd ?
        out_size        dd ?
ends

struct  IRQH
        list            LHEAD
        handler         dd ?   ;handler roututine
        data            dd ?   ;user-specific data
        num_ints        dd ?   ;how many times handled
ends