Subversion Repositories Kolibri OS

Rev

Rev 10002 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;
  3. ;; Copyright (C) KolibriOS team 2004-2024. All rights reserved.
  4. ;; Kernel programmers are acknowledged in CREDITS.TXT
  5. ;;
  6. ;; Data in this file was originally part of MenuetOS project which is
  7. ;; distributed under the terms of GNU GPL. It is modified and redistributed as
  8. ;; part of KolibriOS project under the terms of GNU GPL.
  9. ;;
  10. ;; Copyright (C) MenuetOS 2000-2004 Ville Mikael Turjanmaa
  11. ;;
  12. ;; KolibriOS is distributed in the hope that it will be useful, but WITHOUT ANY
  13. ;; WARRANTY. No author or distributor accepts responsibility to anyone for the
  14. ;; consequences of using it or for whether it serves any particular purpose or
  15. ;; works at all, unless he says so in writing. Refer to the GNU General Public
  16. ;; License (the "GPL") for full details.
  17. ;
  18. ;; Everyone is granted permission to copy, modify and redistribute KolibriOS,
  19. ;; but only under the conditions described in the GPL. A copy of this license
  20. ;; is supposed to have been given to you along with KolibriOS so you can know
  21. ;; your rights and responsibilities. It should be in a file named COPYING.
  22. ;; Among other things, the copyright notice and this notice must be preserved
  23. ;; on all copies.
  24. ;;
  25. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  26.  
  27. format binary as "mnt"
  28.  
  29. include 'macros.inc'
  30. include 'struct.inc'
  31.  
  32. $Revision: 10010 $
  33.  
  34.  
  35. USE_COM_IRQ     = 1      ; make irq 3 and irq 4 available for PCI devices
  36. VESA_1_2_VIDEO  = 0      ; enable vesa 1.2 bank switch functions
  37.  
  38. ; Enabling the next line will enable serial output console
  39. ;debug_com_base  = 0x3f8  ; 0x3f8 is com1, 0x2f8 is com2, 0x3e8 is com3,
  40.                           ; 0x2e8 is com4, no irq's are used
  41.  
  42. include "proc32.inc"
  43. include "kglobals.inc"
  44. include "lang.inc"
  45. include "encoding.inc"
  46.  
  47. include "const.inc"
  48.  
  49. iglobal
  50. ; The following variable, if equal to 1, duplicates debug output to the screen.
  51. debug_direct_print db 0
  52. ; Start the first app (LAUNCHER) after kernel is loaded? (1=yes, 2 or 0=no)
  53. launcher_start db 1
  54. endg
  55.  
  56. max_processes  =  255
  57. tss_step       =  128 + 8192 ; tss & i/o - 65535 ports, * 256=557056*4
  58.  
  59. os_stack       =  os_data_l - gdts    ; GDTs
  60. os_code        =  os_code_l - gdts
  61. graph_data     =  3 + graph_data_l - gdts
  62. tss0           =  tss0_l - gdts
  63. app_code       =  3 + app_code_l - gdts
  64. app_data       =  3 + app_data_l - gdts
  65. app_tls        =  3 + tls_data_l - gdts
  66. pci_code_sel   =  pci_code_32-gdts
  67. pci_data_sel   =  pci_data_32-gdts
  68.  
  69.  
  70. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  71. ;;
  72. ;;   Included files:
  73. ;;
  74. ;;   Kernel16.inc
  75. ;;    - Booteng.inc   English text for bootup
  76. ;;    - Bootcode.inc  Hardware setup
  77. ;;    - Pci16.inc     PCI functions
  78. ;;
  79. ;;   Kernel32.inc
  80. ;;    - Sys32.inc     Process management
  81. ;;    - Shutdown.inc  Shutdown and restart
  82. ;;    - Fat32.inc     Read / write hd
  83. ;;    - Vesa12.inc    Vesa 1.2 driver
  84. ;;    - Vesa20.inc    Vesa 2.0 driver
  85. ;;    - Vga.inc       VGA driver
  86. ;;    - Stack.inc     Network interface
  87. ;;    - Mouse.inc     Mouse pointer
  88. ;;    - Scincode.inc  Window skinning
  89. ;;    - Pci32.inc     PCI functions
  90. ;;
  91. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  92.  
  93. ; That is a loading and initialization code that also draws the blue screen
  94. ; menu with svn revision number near top right corner of the screen.
  95. include "bootbios.inc"
  96.  
  97. use32
  98. org $+KERNEL_BASE
  99.  
  100. align 4
  101. B32:
  102.         mov     ax, os_stack       ; Selector for os
  103.         mov     ds, ax
  104.         mov     es, ax
  105.         mov     fs, ax
  106.         mov     gs, ax
  107.         mov     ss, ax
  108.         mov     esp, TMP_STACK_TOP       ; Set stack
  109.  
  110. ; CLEAR 0x280000 - HEAP_BASE
  111.  
  112.         xor     eax, eax
  113.         mov     edi, CLEAN_ZONE
  114.         mov     ecx, (HEAP_BASE - OS_BASE - CLEAN_ZONE) / 4
  115.         cld
  116.         rep stosd
  117.  
  118. ; Clear kernel undefined globals and some more (what exactly?)
  119.         mov     edi, endofcode - OS_BASE
  120.         mov     ecx, 0x90000    ; why uglobals_size isn't enough?
  121.         sub     ecx, edi
  122.         shr     ecx, 2
  123.         rep stosd
  124.  
  125. ; SAVE & CLEAR 0-0xffff
  126.  
  127.         mov     edi, 0x1000
  128.         mov     ecx, 0x8000 / 4
  129.         rep stosd
  130.         mov     edi, 0xa000
  131.         mov     ecx, 0x6000 / 4
  132.         rep stosd
  133.  
  134.         call    test_cpu
  135.         bts     [cpu_caps - OS_BASE], CAPS_TSC    ;force use rdtsc
  136.  
  137.         call    acpi_locate
  138.         call    init_BIOS32
  139. ; MEMORY MODEL
  140.         call    mem_test
  141.         call    init_mem
  142.         call    init_page_map
  143.  
  144. ; ENABLE PAGING
  145.  
  146.         mov     eax, sys_proc - OS_BASE + PROC.pdt_0
  147.         mov     cr3, eax
  148.  
  149.         mov     eax, cr0
  150.         or      eax, CR0_PG + CR0_WP
  151.         mov     cr0, eax
  152.  
  153.         lgdt    [gdts]
  154.         jmp     pword os_code:high_code
  155.  
  156. align 4
  157. bios32_entry    dd ?
  158. tmp_page_tabs   dd ?
  159. use16
  160. ap_init16:
  161.         cli
  162.         lgdt    [cs:gdts_ap - ap_init16]
  163.         mov     eax, [cs:cr3_ap - ap_init16]
  164.         mov     cr3, eax
  165.         mov     eax, [cs:cr4_ap - ap_init16]
  166.         mov     cr4, eax
  167.         mov     eax, CR0_PE + CR0_PG + CR0_WP
  168.         mov     cr0, eax
  169.         jmp     pword os_code:ap_init_high
  170. align 16
  171. gdts_ap:
  172.         dw     gdte-gdts-1
  173.         dd     gdts
  174.         dw     0
  175. cr3_ap  dd     ?
  176. cr4_ap  dd     ?
  177. ap_init16_size = $ - ap_init16
  178. use32
  179.  
  180. __DEBUG__ fix 1
  181. __DEBUG_LEVEL__ fix 1
  182. include 'init.inc'
  183.  
  184. org OS_BASE+$
  185.  
  186. include 'fdo.inc'
  187.  
  188. align 4
  189. high_code:
  190.         mov     ax, os_stack
  191.         mov     bx, app_data
  192.         mov     cx, app_tls
  193.         mov     ss, ax
  194.         add     esp, OS_BASE
  195.  
  196.         mov     ds, bx
  197.         mov     es, bx
  198.         mov     fs, cx
  199.         mov     gs, bx
  200.  
  201.         xor     eax, eax
  202.         mov     ebx, 0xFFFFF000 + PG_SHARED + PG_NOCACHE + PG_UWR
  203.         bt      [cpu_caps], CAPS_PAT
  204.         setc    al
  205.         shl     eax, 7
  206.         or      ebx, eax
  207.  
  208.         mov     eax, PG_GLOBAL
  209.         bt      [cpu_caps], CAPS_PGE
  210.         jnc     @F
  211.  
  212.         or      [sys_proc + PROC.pdt_0 + (OS_BASE shr 20)], eax
  213.         or      ebx, eax
  214.  
  215.         mov     eax, cr4
  216.         or      eax, CR4_PGE
  217.         mov     cr4, eax
  218. @@:
  219.         mov     [pte_valid_mask], ebx
  220.  
  221.         xor     eax, eax
  222.         mov     [sys_proc + PROC.pdt_0], eax
  223.         mov     [sys_proc + PROC.pdt_0+4], eax
  224.  
  225.         mov     eax, cr3
  226.         mov     cr3, eax          ; flush TLB
  227.  
  228.         mov     ecx, pg_data.mutex
  229.         call    mutex_init
  230.  
  231.         mov     ecx, disk_list_mutex
  232.         call    mutex_init
  233.  
  234.         mov     ecx, keyboard_list_mutex
  235.         call    mutex_init
  236.  
  237.         mov     ecx, unpack_mutex
  238.         call    mutex_init
  239.  
  240.         mov     ecx, application_table_mutex
  241.         call    mutex_init
  242.  
  243.         mov     ecx, ide_mutex
  244.         call    mutex_init
  245.         mov     ecx, ide_channel1_mutex
  246.         call    mutex_init
  247.         mov     ecx, ide_channel2_mutex
  248.         call    mutex_init
  249.         mov     ecx, ide_channel3_mutex
  250.         call    mutex_init
  251.         mov     ecx, ide_channel4_mutex
  252.         call    mutex_init
  253.         mov     ecx, ide_channel5_mutex
  254.         call    mutex_init
  255.         mov     ecx, ide_channel6_mutex
  256.         call    mutex_init
  257. ;-----------------------------------------------------------------------------
  258. ; SAVE REAL MODE VARIABLES
  259. ;-----------------------------------------------------------------------------
  260. ; --------------- APM ---------------------
  261.  
  262. ; init selectors
  263.         mov     ebx, [BOOT.apm_entry]        ; offset of APM entry point
  264.         movzx   eax, word [BOOT.apm_code_32] ; real-mode segment base address of
  265.                                              ; protected-mode 32-bit code segment
  266.         movzx   ecx, word [BOOT.apm_code_16] ; real-mode segment base address of
  267.                                              ; protected-mode 16-bit code segment
  268.         movzx   edx, word [BOOT.apm_data_16] ; real-mode segment base address of
  269.                                              ; protected-mode 16-bit data segment
  270.  
  271.         shl     eax, 4
  272.         mov     [dword apm_code_32 + 2], ax
  273.         shr     eax, 16
  274.         mov     [dword apm_code_32 + 4], al
  275.  
  276.         shl     ecx, 4
  277.         mov     [dword apm_code_16 + 2], cx
  278.         shr     ecx, 16
  279.         mov     [dword apm_code_16 + 4], cl
  280.  
  281.         shl     edx, 4
  282.         mov     [dword apm_data_16 + 2], dx
  283.         shr     edx, 16
  284.         mov     [dword apm_data_16 + 4], dl
  285.  
  286.         mov     dword[apm_entry], ebx
  287.         mov     word [apm_entry + 4], apm_code_32 - gdts
  288.  
  289.         mov     eax, dword[BOOT.apm_version]   ; version & flags
  290.         mov     [apm_vf], eax
  291. ; -----------------------------------------
  292.         mov     al, [BOOT.dma]            ; DMA access
  293.         mov     [allow_dma_access], al
  294.  
  295.         mov     al, [BOOT.debug_print]    ; If nonzero, duplicates debug output to the screen
  296.         mov     [debug_direct_print], al
  297.  
  298.         mov     al, [BOOT.launcher_start] ; Start the first app (LAUNCHER) after kernel is loaded?
  299.         mov     [launcher_start], al
  300.  
  301.         mov     eax, [BOOT.devicesdat_size]
  302.         mov     [acpi_dev_size], eax
  303.         mov     eax, [BOOT.devicesdat_data]
  304.         mov     [acpi_dev_data], eax
  305.  
  306.         mov     esi, BOOT.bios_hd
  307.         movzx   ecx, byte [esi-1]
  308.         mov     [NumBiosDisks], ecx
  309.         mov     edi, BiosDisksData
  310.         shl     ecx, 2
  311.         rep movsd
  312.  
  313. ; -------- Fast System Call init ----------
  314. ; Intel SYSENTER/SYSEXIT (AMD CPU support it too)
  315.         bt      [cpu_caps], CAPS_SEP
  316.         jnc     .SEnP  ; SysEnter not Present
  317.         xor     edx, edx
  318.         mov     ecx, MSR_SYSENTER_CS
  319.         mov     eax, os_code
  320.         wrmsr
  321.         mov     ecx, MSR_SYSENTER_ESP
  322. ;           mov eax, sysenter_stack ; Check it
  323.         xor     eax, eax
  324.         wrmsr
  325.         mov     ecx, MSR_SYSENTER_EIP
  326.         mov     eax, sysenter_entry
  327.         wrmsr
  328. .SEnP:
  329. ; AMD SYSCALL/SYSRET
  330.         cmp     byte[cpu_vendor], 'A'
  331.         jne     .noSYSCALL
  332.         mov     eax, 0x80000001
  333.         cpuid
  334.         test    edx, 0x800  ; bit_11 - SYSCALL/SYSRET support
  335.         jz      .noSYSCALL
  336.         mov     ecx, MSR_AMD_EFER
  337.         rdmsr
  338.         or      eax, 1 ; bit_0 - System Call Extension (SCE)
  339.         wrmsr
  340.  
  341.         ; !!!! It`s dirty hack, fix it !!!
  342.         ; Bits of EDX :
  343.         ; Bit 31–16 During the SYSRET instruction, this field is copied into the CS register
  344.         ;  and the contents of this field, plus 8, are copied into the SS register.
  345.         ; Bit 15–0 During the SYSCALL instruction, this field is copied into the CS register
  346.         ;  and the contents of this field, plus 8, are copied into the SS register.
  347.  
  348.         ; mov   edx, (os_code + 16) * 65536 + os_code
  349.         mov     edx, 0x1B0008
  350.  
  351.         mov     eax, syscall_entry
  352.         mov     ecx, MSR_AMD_STAR
  353.         wrmsr
  354. .noSYSCALL:
  355. ; -----------------------------------------
  356.         stdcall alloc_page
  357.         stdcall map_page, tss-0xF80, eax, PG_SWR
  358.         stdcall alloc_page
  359.         stdcall map_page, tss+0x80, eax, PG_SWR
  360.         stdcall alloc_page
  361.         stdcall map_page, tss+0x1080, eax, PG_SWR
  362.  
  363. ; LOAD IDT
  364.  
  365.         call    build_interrupt_table ;lidt is executed
  366.           ;lidt [idtreg]
  367.  
  368.         call    init_kernel_heap
  369.         call    init_fpu
  370.         mov     eax, [xsave_area_size]
  371.         lea     eax, [eax*2 + RING0_STACK_SIZE*2]
  372.         stdcall kernel_alloc, eax
  373.         mov     [os_stack_seg], eax
  374.  
  375.         lea     esp, [eax + RING0_STACK_SIZE]
  376.  
  377.         mov     [tss._ss0], os_stack
  378.         mov     [tss._esp0], esp
  379.         mov     [tss._esp], esp
  380.         mov     [tss._cs], os_code
  381.         mov     [tss._ss], os_stack
  382.         mov     [tss._ds], app_data
  383.         mov     [tss._es], app_data
  384.         mov     [tss._fs], app_data
  385.         mov     [tss._gs], app_data
  386.         mov     [tss._io], 128
  387. ;Add IO access table - bit array of permitted ports
  388.         mov     edi, tss._io_map_0
  389.         xor     eax, eax
  390.         not     eax
  391.         mov     ecx, 8192/4
  392.         rep stosd                    ; access to 4096*8=65536 ports
  393.  
  394.         mov     ax, tss0
  395.         ltr     ax
  396.  
  397.         mov     eax, sys_proc
  398.         list_init eax
  399.         add     eax, PROC.thr_list
  400.         list_init eax
  401.  
  402.         call    init_video
  403.         call    init_pat_mtrr
  404.         mov     [LFBAddress], LFB_BASE
  405.         mov     ecx, bios_fb
  406.         call    set_framebuffer
  407.         call    init_malloc
  408.  
  409.         stdcall alloc_kernel_space, 0x50000         ; FIXME check size
  410.         mov     [default_io_map], eax
  411.  
  412.         add     eax, 0x2000
  413.         mov     [ipc_tmp], eax
  414.         mov     ebx, 0x1000
  415.  
  416.         add     eax, 0x40000
  417.         mov     [proc_mem_map], eax
  418.  
  419.         add     eax, 0x8000
  420.         mov     [proc_mem_pdir], eax
  421.  
  422.         add     eax, ebx
  423.         mov     [proc_mem_tab], eax
  424.  
  425.         add     eax, ebx
  426.         mov     [tmp_task_ptab], eax
  427.  
  428.         add     eax, ebx
  429.         mov     [ipc_pdir], eax
  430.  
  431.         add     eax, ebx
  432.         mov     [ipc_ptab], eax
  433.  
  434.         stdcall kernel_alloc, (unpack.LZMA_BASE_SIZE + (unpack.LZMA_LIT_SIZE shl \
  435.                 (unpack.lc + unpack.lp)))*4
  436.  
  437.         mov     [unpack.p], eax
  438.  
  439.         call    init_events
  440.         mov     eax, srv.fd - SRV.fd
  441.         mov     [srv.fd], eax
  442.         mov     [srv.bk], eax
  443.  
  444. ;Set base of graphic segment to linear address of LFB
  445.         mov     eax, [LFBAddress]         ; set for gs
  446.         mov     [graph_data_l+2], ax
  447.         shr     eax, 16
  448.         mov     [graph_data_l+4], al
  449.         mov     [graph_data_l+7], ah
  450.  
  451.         stdcall kernel_alloc, [_display.win_map_size]
  452.         mov     [_display.win_map], eax
  453.  
  454.         xor     eax, eax
  455.         inc     eax
  456.  
  457. ; set background
  458.  
  459.         mov     [BgrDrawMode], eax
  460.         mov     [BgrDataWidth], eax
  461.         mov     [BgrDataHeight], eax
  462.         mov     [mem_BACKGROUND], 4
  463.         mov     [img_background], static_background_data
  464.  
  465. ; set clipboard
  466.  
  467.         xor     eax, eax
  468.         mov     [clipboard_slots], eax
  469.         mov     [clipboard_write_lock], eax
  470.         stdcall kernel_alloc, PAGE_SIZE
  471.         test    eax, eax
  472.         jnz     @f
  473.  
  474.         dec     eax
  475. @@:
  476.         mov     [clipboard_main_list], eax
  477.  
  478.         call    check_acpi
  479.  
  480.         mov     eax, [hpet_base]
  481.         test    eax, eax
  482.         jz      @F
  483.         stdcall map_io_mem, [hpet_base], 1024, PG_GLOBAL + PAT_UC + PG_SWR
  484.         mov     [hpet_base], eax
  485.         mov     eax, [eax+HPET_ID]
  486.         DEBUGF  1, "K : HPET caps %x\n", eax
  487.         call    init_hpet
  488. @@:
  489. ; SET UP OS TASK
  490.  
  491.         mov     esi, boot_setostask
  492.         call    boot_log
  493.  
  494.         mov     edi, sys_proc + PROC.heap_lock
  495.         mov     ecx, (PROC.ht_free - PROC.heap_lock)/4
  496.  
  497.         xor     eax, eax
  498.         cld
  499.         rep stosd
  500.  
  501.         mov     [edi], dword (PROC.pdt_0 - PROC.htab)/4 - 3
  502.         mov     [edi+4], dword 3           ;reserve handles for stdin stdout and stderr
  503.         mov     ecx, (PROC.pdt_0 - PROC.htab)/4
  504.         add     edi, 8
  505.         inc     eax
  506. @@:
  507.         stosd
  508.         inc     eax
  509.         cmp     eax, ecx
  510.         jbe     @B
  511.  
  512.         mov     [sys_proc + PROC.pdt_0_phys], sys_proc - OS_BASE + PROC.pdt_0
  513.  
  514.         mov     eax, -1
  515.         mov     edi, thr_slot_map+4
  516.         mov     [edi-4], dword 0xFFFFFFF8
  517.         stosd
  518.         stosd
  519.         stosd
  520.         stosd
  521.         stosd
  522.         stosd
  523.         stosd
  524.  
  525.         mov     [current_process], sys_proc
  526.  
  527.         ; set all the threads state to free
  528.         mov     edi, SLOT_BASE
  529.         movi    eax, TSTATE_FREE
  530.         movi    ecx, max_processes
  531. @@:
  532.         mov     [edi+APPDATA.state], TSTATE_FREE
  533.         add     edi, sizeof.APPDATA
  534.         dec     ecx
  535.         jns     @b
  536.  
  537.         mov     edx, SLOT_BASE + sizeof.APPDATA*1
  538.         mov     ebx, [os_stack_seg]
  539.         add     ebx, RING0_STACK_SIZE
  540.         add     ebx, [xsave_area_size]
  541.         call    setup_os_slot
  542.         mov     dword [edx], 'IDLE'
  543.         sub     [edx + APPDATA.saved_esp], 4
  544.         mov     eax, [edx + APPDATA.saved_esp]
  545.         mov     dword [eax], idle_thread
  546.         mov     ecx, IDLE_PRIORITY
  547.         call    scheduler_add_thread
  548.  
  549.         mov     edx, SLOT_BASE + sizeof.APPDATA*2
  550.         mov     ebx, [os_stack_seg]
  551.         call    setup_os_slot
  552.         mov     dword [edx], 'OS'
  553.         xor     ecx, ecx
  554.         call    scheduler_add_thread
  555.  
  556.         mov     [current_slot_idx], 2
  557.         mov     [thread_count], 2
  558.         mov     [current_slot], SLOT_BASE + sizeof.APPDATA*2
  559.  
  560. ; Move other CPUs to deep sleep, if it is useful
  561. uglobal
  562. use_mwait_for_idle db 0
  563. endg
  564.         cmp     [cpu_vendor+8], 'ntel'
  565.         jnz     .no_wake_cpus
  566.         bt      [cpu_caps+4], CAPS_MONITOR-32
  567.         jnc     .no_wake_cpus
  568.         dbgstr 'using mwait for idle loop'
  569.         inc     [use_mwait_for_idle]
  570.         mov     ebx, [cpu_count]
  571.         cmp     ebx, 1
  572.         jbe     .no_wake_cpus
  573.         call    create_trampoline_pgmap
  574.         mov     [cr3_ap + OS_BASE], eax
  575.         mov     eax, cr4
  576.         mov     [cr4_ap + OS_BASE], eax
  577.         mov     esi, OS_BASE + ap_init16
  578.         mov     edi, OS_BASE + 8000h
  579.         mov     ecx, (ap_init16_size + 3) / 4
  580.         rep movsd
  581.         mov     eax, [LAPIC_BASE]
  582.         test    eax, eax
  583.         jnz     @f
  584.         stdcall map_io_mem, [acpi_lapic_base], 0x1000, PG_GLOBAL + PG_NOCACHE + PG_SWR
  585.         mov     [LAPIC_BASE], eax
  586. @@:
  587.         lea     edi, [eax + APIC_ICRL]
  588.         mov     esi, smpt+4
  589.         dec     ebx
  590. .wake_cpus_loop:
  591.         lodsd
  592.         push    esi
  593.         xor     esi, esi
  594.         inc     esi
  595.         shl     eax, 24
  596.         mov     [edi+10h], eax
  597. ; assert INIT IPI
  598.         mov     dword [edi], 0C500h
  599.         call    delay_ms
  600. @@:
  601.         test    dword [edi], 1000h
  602.         jnz     @b
  603. ; deassert INIT IPI
  604.         mov     dword [edi], 8500h
  605.         call    delay_ms
  606. @@:
  607.         test    dword [edi], 1000h
  608.         jnz     @b
  609. ; send STARTUP IPI
  610.         mov     dword [edi], 600h + (8000h shr 12)
  611.         call    delay_ms
  612. @@:
  613.         test    dword [edi], 1000h
  614.         jnz     @b
  615.         pop     esi
  616.         dec     ebx
  617.         jnz     .wake_cpus_loop
  618.         mov     eax, [cpu_count]
  619.         dec     eax
  620. @@:
  621.         cmp     [ap_initialized], eax
  622.         jnz     @b
  623.         mov     eax, [cr3_ap + OS_BASE]
  624.         call    free_page
  625. .no_wake_cpus:
  626.  
  627. ; REDIRECT ALL IRQ'S TO INT'S 0x20-0x2f
  628.         mov     esi, boot_initirq
  629.         call    boot_log
  630.         call    init_irqs
  631.  
  632.         mov     esi, boot_picinit
  633.         call    boot_log
  634.         call    PIC_init
  635.  
  636.         mov     esi, boot_v86machine
  637.         call    boot_log
  638. ; Initialize system V86 machine
  639.         call    init_sys_v86
  640.  
  641.         mov     esi, boot_inittimer
  642.         call    boot_log
  643. ; Initialize system timer (IRQ0)
  644.         call    PIT_init
  645.  
  646. ; Register ramdisk file system
  647. if ~ defined extended_primary_loader
  648.         cmp     [BOOT.rd_load_from], RD_LOAD_FROM_HD    ; will be loaded later
  649.         je      @f
  650. end if
  651.         cmp     [BOOT.rd_load_from], RD_LOAD_FROM_NONE
  652.         je      @f
  653.         call    register_ramdisk
  654. ;--------------------------------------
  655. @@:
  656.         mov     esi, boot_initapic
  657.         call    boot_log
  658. ; Try to Initialize APIC
  659.         call    APIC_init
  660.  
  661.         mov     esi, boot_enableirq
  662.         call    boot_log
  663. ; Enable timer IRQ (IRQ0) and co-processor IRQ (IRQ13)
  664. ; they are used: when partitions are scanned, hd_read relies on timer
  665.         call    unmask_timer
  666.         ; Prevent duplicate timer IRQs in APIC mode
  667.         cmp     [irq_mode], IRQ_APIC
  668.         jz      @f
  669.         stdcall enable_irq, 2               ; @#$%! PIC
  670. @@:
  671.         stdcall enable_irq, 13              ; co-processor
  672.  
  673. ; Setup serial output console (if enabled)
  674. if defined debug_com_base
  675.  
  676.         ; reserve port so nobody else will use it
  677.         xor     ebx, ebx
  678.         mov     ecx, debug_com_base
  679.         mov     edx, debug_com_base+7
  680.         call    r_f_port_area
  681.  
  682.         ; enable Divisor latch
  683.         mov     dx, debug_com_base+3
  684.         mov     al, 1 shl 7
  685.         out     dx, al
  686.  
  687.         ; Set speed to 115200 baud (max speed)
  688.         mov     dx, debug_com_base
  689.         mov     al, 0x01
  690.         out     dx, al
  691.  
  692.         mov     dx, debug_com_base+1
  693.         mov     al, 0x00
  694.         out     dx, al
  695.  
  696.         ; No parity, 8bits words, one stop bit, dlab bit back to 0
  697.         mov     dx, debug_com_base+3
  698.         mov     al, 3
  699.         out     dx, al
  700.  
  701.         ; disable interrupts
  702.         mov     dx, debug_com_base+1
  703.         mov     al, 0
  704.         out     dx, al
  705.  
  706.         ; clear +  enable fifo (64 bits)
  707.         mov     dx, debug_com_base+2
  708.         mov     al, 0x7 + 1 shl 5
  709.         out     dx, al
  710.  
  711. end if
  712.  
  713.  
  714. ;-----------------------------------------------------------------------------
  715. ; show SVN version of kernel on the message board
  716. ;-----------------------------------------------------------------------------
  717.         mov     eax, [version_inf.rev]
  718.         DEBUGF  1, "K : kernel SVN r%d\n", eax
  719. ;-----------------------------------------------------------------------------
  720. ; show CPU count on the message board
  721. ;-----------------------------------------------------------------------------
  722.         mov     eax, [cpu_count]
  723.         test    eax, eax
  724.         jnz     @F
  725.         mov     al, 1                             ; at least one CPU
  726. @@:
  727.         DEBUGF  1, "K : %d CPU detected\n", eax
  728. ;-----------------------------------------------------------------------------
  729. ; detect Floppy drives
  730. ;-----------------------------------------------------------------------------
  731.         mov     esi, boot_detectfloppy
  732.         call    boot_log
  733. include 'detect/dev_fd.inc'
  734. ;-----------------------------------------------------------------------------
  735. ; create pci-devices list
  736. ;-----------------------------------------------------------------------------
  737.         mov     [pci_access_enabled], 1
  738.         call    pci_enum
  739. ;-----------------------------------------------------------------------------
  740. ; initialisation IDE ATA code
  741. ;-----------------------------------------------------------------------------
  742. include 'detect/init_ata.inc'
  743. ;-----------------------------------------------------------------------------
  744. ; initialisation AHCI code
  745. ;-----------------------------------------------------------------------------
  746.         jmp     ahci_code_end
  747. include 'blkdev/ahci.inc'
  748. ahci_code_end:
  749.         call    ahci_init
  750. ;-----------------------------------------------------------------------------
  751. if 0
  752.         mov     ax, [BOOT.sys_disk]
  753.         cmp     ax, 'r1'; if using not ram disk, then load librares and parameters {SPraid.simba}
  754.         je      no_lib_load
  755.  
  756.         mov     esi, boot_loadlibs
  757.         call    boot_log
  758. ; LOADING LIBRARES
  759.         stdcall dll.Load, @IMPORT           ; loading librares for kernel (.obj files)
  760.         call    load_file_parse_table       ; prepare file parse table
  761.         call    set_kernel_conf             ; configure devices and gui
  762. no_lib_load:
  763. end if
  764.  
  765. ; Display APIC status
  766.         mov     esi, boot_APIC_found
  767.         cmp     [irq_mode], IRQ_APIC
  768.         je      @f
  769.         mov     esi, boot_APIC_nfound
  770. @@:
  771.         call    boot_log
  772.  
  773. ; PRINT AMOUNT OF MEMORY
  774.         mov     esi, boot_memdetect
  775.         call    boot_log
  776.  
  777.         movzx   ecx, word [boot_y]
  778.         if lang eq ru
  779.         or      ecx, (10+30*6) shl 16
  780.         else if lang eq sp
  781.         or      ecx, (10+33*6) shl 16
  782.         else
  783.         or      ecx, (10+29*6) shl 16
  784.         end if
  785.         sub     ecx, 10
  786.         mov     edx, 0xFFFFFF
  787.         mov     ebx, [MEM_AMOUNT]
  788.         shr     ebx, 20
  789.         xor     edi, edi
  790.         mov     eax, 0x00040000
  791.         inc     edi
  792.         call    display_number_force
  793.  
  794. ; BUILD SCHEDULER
  795.  
  796. ;        call    build_scheduler; sys32.inc
  797.  
  798. ;        mov     esi, boot_devices
  799. ;        call    boot_log
  800.  
  801. include "detect/vortex86.inc"                     ; Vortex86 SoC detection code
  802.  
  803.         stdcall load_pe_driver, szVidintel, 0
  804.  
  805.         call    usb_init
  806.  
  807. ; SET PRELIMINARY WINDOW STACK AND POSITIONS
  808.  
  809.         mov     esi, boot_windefs
  810.         call    boot_log
  811.         call    set_window_defaults
  812.  
  813. ; SET BACKGROUND DEFAULTS
  814.  
  815.         mov     esi, boot_bgr
  816.         call    boot_log
  817.         call    init_background
  818.         call    calculatebackground
  819.  
  820. ; RESERVE SYSTEM IRQ'S JA PORT'S
  821.  
  822.         mov     esi, boot_resirqports
  823.         call    boot_log
  824.         call    reserve_irqs_ports
  825.  
  826.         mov     [SLOT_BASE + APPDATA.window], window_data
  827.         mov     [SLOT_BASE + sizeof.APPDATA + APPDATA.window], window_data + sizeof.WDATA
  828.         mov     [SLOT_BASE + sizeof.APPDATA*2 + APPDATA.window], window_data + sizeof.WDATA*2
  829.         mov     [window_data + WDATA.thread], SLOT_BASE
  830.         mov     [window_data + sizeof.WDATA + WDATA.thread], SLOT_BASE + sizeof.APPDATA
  831.         mov     [window_data + sizeof.WDATA*2 + WDATA.thread], SLOT_BASE + sizeof.APPDATA*2
  832.  
  833.         call    init_display
  834.         mov     eax, [def_cursor]
  835.         mov     [window_data + sizeof.WDATA + WDATA.cursor], eax
  836.         mov     [window_data + sizeof.WDATA*2 + WDATA.cursor], eax
  837.  
  838. ; PRINT CPU FREQUENCY
  839.  
  840.         mov     esi, boot_cpufreq
  841.         call    boot_log
  842.  
  843.         cli
  844.         mov     ebx, [hpet_base]
  845.         test    ebx, ebx
  846.         jz      @F
  847.         mov     ebx, [ebx + HPET_COUNTER]
  848.  
  849.         rdtsc
  850.         mov     ecx, 1000
  851.         sub     eax, [hpet_tsc_start]
  852.         sbb     edx, [hpet_tsc_start + 4]
  853.         shld    edx, eax, 10
  854.         shl     eax, 10
  855.         mov     esi, eax
  856.         mov     eax, edx
  857.         mul     ecx
  858.         xchg    eax, esi
  859.         mul     ecx
  860.         adc     edx, esi
  861.         div     ebx
  862.         mul     ecx
  863.         div     [hpet_period]
  864.         mul     ecx
  865.         DEBUGF  1, "K : cpu frequency %u Hz\n", eax
  866.         jmp     .next
  867. @@:
  868.         rdtsc
  869.         mov     ecx, eax
  870.         mov     esi, 250            ; wait 1/4 a second
  871.         call    delay_ms
  872.         rdtsc
  873.  
  874.         sub     eax, ecx
  875.         xor     edx, edx
  876.         shld    edx, eax, 2
  877.         shl     eax, 2
  878. .next:
  879.         mov     dword [cpu_freq], eax
  880.         mov     dword [cpu_freq + 4], edx
  881.         mov     ebx, 1000000
  882.         div     ebx
  883.         mov     ebx, eax
  884.  
  885.         movzx   ecx, word [boot_y]
  886.         if lang eq ru
  887.         add     ecx, (10+19*6) shl 16 - 10
  888.         else if lang eq sp
  889.         add     ecx, (10+25*6) shl 16 - 10
  890.         else
  891.         add     ecx, (10+17*6) shl 16 - 10
  892.         end if
  893.  
  894.         mov     edx, 0xFFFFFF
  895.         xor     edi, edi
  896.         mov     eax, 0x00040000
  897.         inc     edi
  898.         call    display_number_force
  899.  
  900. ; SET VARIABLES
  901.  
  902.         call    set_variables
  903.  
  904. ; STACK AND FDC
  905.  
  906.         call    stack_init
  907.         call    fdc_init
  908.  
  909. ; PALETTE FOR 320x200 and 640x480 16 col
  910.  
  911.         cmp     [SCR_MODE], word 0x12
  912.         jne     no_pal_vga
  913.         mov     esi, boot_pal_vga
  914.         call    boot_log
  915.         call    paletteVGA
  916.       no_pal_vga:
  917.  
  918.         cmp     [SCR_MODE], word 0x13
  919.         jne     no_pal_ega
  920.         mov     esi, boot_pal_ega
  921.         call    boot_log
  922.         call    palette320x200
  923.       no_pal_ega:
  924.  
  925. ; LOAD DEFAULT SKIN
  926.  
  927.         call    load_default_skin
  928.  
  929. ; Protect I/O permission map
  930.  
  931.         mov     esi, [default_io_map]
  932.         stdcall map_page, esi, [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map], PG_READ
  933.         add     esi, 0x1000
  934.         stdcall map_page, esi, [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map + 4], PG_READ
  935.  
  936.         stdcall map_page, tss._io_map_0, \
  937.                 [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map], PG_READ
  938.         stdcall map_page, tss._io_map_1, \
  939.                 [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map + 4], PG_READ
  940.  
  941. ; SET KEYBOARD PARAMETERS
  942.         mov     al, 0xf6       ; reset keyboard, scan enabled
  943.         call    kb_write_wait_ack
  944.         test    ah, ah
  945.         jnz     .no_keyboard
  946.  
  947. iglobal
  948. align 4
  949. ps2_keyboard_functions:
  950.         dd      .end - $
  951.         dd      0       ; no close
  952.         dd      ps2_set_lights
  953. .end:
  954. endg
  955.         stdcall register_keyboard, ps2_keyboard_functions, 0
  956.        ; mov   al, 0xED       ; Keyboard LEDs - only for testing!
  957.        ; call  kb_write_wait_ack
  958.        ; mov   al, 111b
  959.        ; call  kb_write_wait_ack
  960.  
  961.         mov     al, 0xF3     ; set repeat rate & delay
  962.         call    kb_write_wait_ack
  963.         mov     al, 0; 30 250 ;00100010b ; 24 500  ;00100100b  ; 20 500
  964.         call    kb_write_wait_ack
  965.      ;// mike.dld [
  966.         call    set_lights
  967.      ;// mike.dld ]
  968.         stdcall attach_int_handler, 1, irq1, 0
  969.         DEBUGF  1, "K : IRQ1 return code %x\n", eax
  970. .no_keyboard:
  971.  
  972. ; Load PS/2 mouse driver
  973.         mov     esi, boot_setmouse
  974.         call    boot_log
  975.         stdcall load_pe_driver, szPS2MDriver, 0
  976.  
  977. ; LOAD FIRST APPLICATION
  978.         cmp     byte [launcher_start], 1        ; Check if starting LAUNCHER is selected on blue screen (1 = yes)
  979.         jnz     first_app_found
  980.  
  981.         cli
  982.         mov     ebp, firstapp
  983.         call    fs_execute_from_sysdir
  984.         test    eax, eax
  985.         jns     first_app_found
  986.  
  987.         mov     esi, boot_failed
  988.         call    boot_log
  989.  
  990.         mov     eax, 0xDEADBEEF      ; otherwise halt
  991.         hlt
  992.  
  993. first_app_found:
  994.  
  995. ; START MULTITASKING
  996. preboot_blogesc = 0       ; start immediately after bootlog
  997.  
  998. ; A 'All set - press ESC to start' messages if need
  999. if preboot_blogesc
  1000.         mov     esi, boot_tasking
  1001.         call    boot_log
  1002. .bll1:
  1003.         in      al, 0x60        ; wait for ESC key press
  1004.         cmp     al, 129
  1005.         jne     .bll1
  1006. end if
  1007.  
  1008.         mov     [timer_ticks_enable], 1         ; for cd driver
  1009.         sti
  1010.  
  1011.         call    mtrr_validate
  1012.  
  1013.         jmp     osloop
  1014.         ; Fly :)
  1015.  
  1016. uglobal
  1017. align 4
  1018. ap_initialized  dd      0
  1019. endg
  1020.  
  1021. ap_init_high:
  1022.         mov     ax, os_stack
  1023.         mov     bx, app_data
  1024.         mov     cx, app_tls
  1025.         mov     ss, ax
  1026.         mov     ds, bx
  1027.         mov     es, bx
  1028.         mov     fs, cx
  1029.         mov     gs, bx
  1030.         xor     esp, esp
  1031.         mov     eax, sys_proc - OS_BASE + PROC.pdt_0
  1032.         mov     cr3, eax
  1033.         lock inc [ap_initialized]
  1034.         jmp     idle_loop
  1035.  
  1036.  
  1037. include 'unpacker.inc'
  1038.  
  1039. align 4
  1040. boot_log:
  1041.         pushad
  1042.  
  1043.         mov     ebx, 10*65536
  1044.         mov     bx, word [boot_y]
  1045.         add     [boot_y], dword 10
  1046.         mov     ecx, 0x80ffffff; ASCIIZ string with white color
  1047.         xor     edi, edi
  1048.         mov     edx, esi
  1049.         inc     edi
  1050.         call    dtext
  1051.  
  1052.         mov     [novesachecksum], 1000
  1053.         call    checkVga_N13
  1054.  
  1055.         popad
  1056.  
  1057.         ret
  1058.  
  1059. ;-----------------------------------------------------------------------------
  1060. ; Register ramdisk file system
  1061. register_ramdisk:
  1062.         mov     esi, boot_initramdisk
  1063.         call    boot_log
  1064.         call    ramdisk_init
  1065.         ret
  1066.  
  1067. ; in: edx -> APPDATA for OS/IDLE slot
  1068. ; in: ebx = stack base
  1069. proc setup_os_slot
  1070.         xor     eax, eax
  1071.         mov     ecx, sizeof.APPDATA/4
  1072.         mov     edi, edx
  1073.         rep stosd
  1074.  
  1075.         mov     eax, tss+0x80
  1076.         call    get_pg_addr
  1077.         inc     eax
  1078.         mov     [edx + APPDATA.io_map], eax
  1079.         mov     eax, tss+0x1080
  1080.         call    get_pg_addr
  1081.         inc     eax
  1082.         mov     [edx + APPDATA.io_map + 4], eax
  1083.  
  1084.         mov     [edx + APPDATA.pl0_stack], ebx
  1085.         lea     edi, [ebx + RING0_STACK_SIZE]
  1086.         mov     [edx + APPDATA.fpu_state], edi
  1087.         mov     [edx + APPDATA.saved_esp0], edi
  1088.         mov     [edx + APPDATA.saved_esp], edi
  1089.         mov     [edx + APPDATA.terminate_protection], 1 ; make unkillable
  1090.  
  1091.         mov     esi, fpu_data
  1092.         mov     ecx, [xsave_area_size]
  1093.         add     ecx, 3
  1094.         shr     ecx, 2
  1095.         rep movsd
  1096.  
  1097.         lea     eax, [edx + APP_EV_OFFSET]
  1098.         mov     [edx + APPDATA.fd_ev], eax
  1099.         mov     [edx + APPDATA.bk_ev], eax
  1100.  
  1101.         lea     eax, [edx + APP_OBJ_OFFSET]
  1102.         mov     [edx + APPDATA.fd_obj], eax
  1103.         mov     [edx + APPDATA.bk_obj], eax
  1104.  
  1105.         mov     [edx + APPDATA.cur_dir], sysdir_path-2
  1106.  
  1107.         mov     [edx + APPDATA.process], sys_proc
  1108.  
  1109.         lea     ebx, [edx + APPDATA.list]
  1110.         lea     ecx, [sys_proc + PROC.thr_list]
  1111.         list_add_tail ebx, ecx
  1112.  
  1113.         mov     [edx + APPDATA.wnd_number], dh
  1114.         mov     byte [edx + APPDATA.tid], dh
  1115.         movzx   eax, dh
  1116.         shl     eax, BSF sizeof.WDATA
  1117.         add     eax, window_data
  1118.         mov     [edx + APPDATA.window], eax
  1119.  
  1120.         ret
  1121. endp
  1122.  
  1123. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1124. ;                                                                    ;
  1125. ;                    MAIN OS LOOP START                              ;
  1126. ;                                                                    ;
  1127. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1128. align 32
  1129. osloop:
  1130.         mov     edx, osloop_has_work?
  1131.         xor     ecx, ecx
  1132.         call    Wait_events
  1133.         xor     eax, eax
  1134.         xchg    eax, [osloop_nonperiodic_work]
  1135.         test    eax, eax
  1136.         jz      .no_periodic
  1137.  
  1138.         call    __sys_draw_pointer
  1139.         call    window_check_events
  1140.         call    mouse_check_events
  1141.         call    checkmisc
  1142.         call    checkVga_N13
  1143. ;--------------------------------------
  1144. .no_periodic:
  1145.         call    stack_handler
  1146.         call    check_fdd_motor_status
  1147.         call    check_ATAPI_device_event
  1148.         call    check_lights_state
  1149.         call    check_timers
  1150.  
  1151.         jmp     osloop
  1152. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1153. ;                                                                    ;
  1154. ;                      MAIN OS LOOP END                              ;
  1155. ;                                                                    ;
  1156. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1157. proc osloop_has_work?
  1158.         cmp     [osloop_nonperiodic_work], 0
  1159.         jnz     .yes
  1160.         call    stack_handler_has_work?
  1161.         jnz     .yes
  1162.         call    check_fdd_motor_status_has_work?
  1163.         jnz     .yes
  1164.         call    check_ATAPI_device_event_has_work?
  1165.         jnz     .yes
  1166.         call    check_lights_state_has_work?
  1167.         jnz     .yes
  1168.         call    check_timers_has_work?
  1169.         jnz     .yes
  1170. .no:
  1171.         xor     eax, eax
  1172.         ret
  1173. .yes:
  1174.         xor     eax, eax
  1175.         inc     eax
  1176.         ret
  1177. endp
  1178.  
  1179. proc wakeup_osloop
  1180.         mov     [osloop_nonperiodic_work], 1
  1181.         ret
  1182. endp
  1183.  
  1184. uglobal
  1185. align 4
  1186. osloop_nonperiodic_work dd      ?
  1187. endg
  1188.  
  1189. uglobal
  1190. align 64
  1191. idle_addr       rb      64
  1192. endg
  1193.  
  1194. idle_thread:
  1195.         sti
  1196.  
  1197. ; The following code can be executed by all CPUs in the system.
  1198. ; All other parts of the kernel do not expect multi-CPU.
  1199. ; Also, APs don't even have a stack here.
  1200. ; Beware. Don't do anything here. Anything at all.
  1201. idle_loop:
  1202.         cmp     [use_mwait_for_idle], 0
  1203.         jnz     .mwait
  1204.  
  1205. .hlt:
  1206.         hlt
  1207.         jmp     .hlt
  1208.  
  1209. .mwait:
  1210.         mov     eax, idle_addr
  1211.         xor     ecx, ecx
  1212.         xor     edx, edx
  1213.         monitor
  1214.         xor     ecx, ecx
  1215.         mov     eax, 20h        ; or 10h
  1216.         mwait
  1217.         jmp     .mwait
  1218.  
  1219.  
  1220. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1221. ;                                                                      ;
  1222. ;                   INCLUDED SYSTEM FILES                              ;
  1223. ;                                                                      ;
  1224. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1225.  
  1226.  
  1227. include "kernel32.inc"
  1228.  
  1229.  
  1230. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1231. ;                                                                      ;
  1232. ;                       KERNEL FUNCTIONS                               ;
  1233. ;                                                                      ;
  1234. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1235.  
  1236. reserve_irqs_ports:
  1237.  
  1238.  
  1239. ; RESERVE PORTS
  1240.         mov     eax, RESERVED_PORTS
  1241.         mov     ecx, 1
  1242.  
  1243.         mov     [eax], dword 4
  1244.  
  1245.         mov     [eax+16], ecx
  1246.         mov     [eax+16+4], dword 0
  1247.         mov     [eax+16+8], dword 0x2D
  1248.  
  1249.         mov     [eax+32], ecx
  1250.         mov     [eax+32+4], dword 0x30
  1251.         mov     [eax+32+8], dword 0x4D
  1252.  
  1253.         mov     [eax+48], ecx
  1254.         mov     [eax+48+4], dword 0x50
  1255.         mov     [eax+48+8], dword 0xDF
  1256.  
  1257.         mov     [eax+64], ecx
  1258.         mov     [eax+64+4], dword 0xE5
  1259.         mov     [eax+64+8], dword 0xFF
  1260.  
  1261.         ret
  1262.  
  1263.  
  1264. iglobal
  1265.   process_number dd 0x2
  1266. endg
  1267.  
  1268. set_variables:
  1269.  
  1270.         mov     ecx, 0x16                    ; flush port 0x60
  1271. @@:
  1272.         in      al, 0x60
  1273.         loop    @b
  1274.         push    eax
  1275.  
  1276.         mov     ax, [BOOT.y_res]
  1277.         shr     ax, 1
  1278.         shl     eax, 16
  1279.         mov     ax, [BOOT.x_res]
  1280.         shr     ax, 1
  1281.         mov     [MOUSE_X], eax
  1282.         call    wakeup_osloop
  1283.  
  1284.         xor     eax, eax
  1285.         mov     [BTN_ADDR], dword BUTTON_INFO ; address of button list
  1286.  
  1287.         mov     byte [KEY_COUNT], al              ; keyboard buffer
  1288.         mov     byte [BTN_COUNT], al              ; button buffer
  1289.  
  1290.         pop     eax
  1291.         ret
  1292. ;-----------------------------------------------------------------------------
  1293.  
  1294. align 4
  1295. display_number:
  1296. ; add check pointers
  1297.         test    bl, bl
  1298.         jz      @f
  1299.         bt      ebx, 30  ; check 30 bit
  1300.         jb      @f
  1301.         stdcall is_region_userspace, ecx, 4
  1302.         jz      @f
  1303.         ret
  1304. @@:
  1305.         jz      @f
  1306.         stdcall is_region_userspace, ecx, 8
  1307.         jz      @f
  1308.         ret
  1309. @@:
  1310.         test    esi, 0x08000000
  1311.         jz      @f
  1312.         stdcall is_region_userspace, edi, 1
  1313.         jz      @f
  1314.         ret
  1315. @@:
  1316. ;It is not optimization
  1317.         mov     eax, ebx
  1318.         mov     ebx, ecx
  1319.         mov     ecx, edx
  1320.         mov     edx, esi
  1321.         mov     esi, edi
  1322. ; eax = print type, al=0 -> ebx is number
  1323. ;                   al=1 -> ebx is pointer
  1324. ;                   ah=0 -> display decimal
  1325. ;                   ah=1 -> display hexadecimal
  1326. ;                   ah=2 -> display binary
  1327. ;                   eax bits 16-21 = number of digits to display (0-32)
  1328. ;                   eax bits 22-31 = reserved
  1329. ;
  1330. ; ebx = number or pointer
  1331. ; ecx = x shl 16 + y
  1332. ; edx = color
  1333.         xor     edi, edi
  1334. display_number_force:
  1335.         push    eax
  1336.         and     eax, 0x3fffffff
  1337.         cmp     eax, 0xffff     ; length > 0 ?
  1338.         pop     eax
  1339.         jge     .cont_displ
  1340.         ret
  1341.    .cont_displ:
  1342.         push    eax
  1343.         and     eax, 0x3fffffff
  1344.         cmp     eax, 61*0x10000  ; length <= 60 ?
  1345.         pop     eax
  1346.         jb      .cont_displ2
  1347.         ret
  1348.    .cont_displ2:
  1349.  
  1350.         pushad
  1351.  
  1352.         cmp     al, 1            ; ecx is a pointer ?
  1353.         jne     @f
  1354.         mov     ebp, ebx
  1355.         add     ebp, 4
  1356.         mov     ebp, [ebp] ;[ebp + std_application_base_address]
  1357.         mov     ebx, [ebx] ;[ebx + std_application_base_address]
  1358. @@:
  1359.         sub     esp, 64
  1360.  
  1361.         test    ah, ah            ; DECIMAL
  1362.         jnz     .no_display_desnum
  1363.  
  1364.         shr     eax, 16
  1365.         and     eax, 0xC03f
  1366. ;     and   eax,0x3f
  1367.         push    eax
  1368.         and     eax, 0x3f
  1369.         mov     edi, esp
  1370.         add     edi, 4+64-1
  1371.         mov     ecx, eax
  1372.         mov     eax, ebx
  1373.         mov     ebx, 10
  1374. @@:
  1375.         xor     edx, edx
  1376.         call    division_64_bits
  1377.         div     ebx
  1378.         add     dl, 48
  1379.         mov     [edi], dl
  1380.         dec     edi
  1381.         loop    @b
  1382.  
  1383.         pop     eax
  1384.         call    normalize_number
  1385.         call    draw_num_text
  1386.         add     esp, 64
  1387.         popad
  1388.         ret
  1389. .no_display_desnum:
  1390.  
  1391.         cmp     ah, 0x01         ; HEXADECIMAL
  1392.         jne     .no_display_hexnum
  1393.  
  1394.         shr     eax, 16
  1395.         and     eax, 0xC03f
  1396. ;     and   eax,0x3f
  1397.         push    eax
  1398.         and     eax, 0x3f
  1399.         mov     edi, esp
  1400.         add     edi, 4+64-1
  1401.         mov     ecx, eax
  1402.         mov     eax, ebx
  1403.         mov     ebx, 16
  1404. @@:
  1405.         xor     edx, edx
  1406.         call    division_64_bits
  1407.         div     ebx
  1408.    ;hexletters = __fdo_hexdigits
  1409.         add     edx, __fdo_hexdigits ;hexletters
  1410.         mov     dl, [edx]
  1411.         mov     [edi], dl
  1412.         dec     edi
  1413.         loop    @b
  1414.  
  1415.         pop     eax
  1416.         call    normalize_number
  1417.         call    draw_num_text
  1418.         add     esp, 64
  1419.         popad
  1420.         ret
  1421. .no_display_hexnum:
  1422.  
  1423.         cmp     ah, 0x02         ; BINARY
  1424.         jne     .no_display_binnum
  1425.  
  1426.         shr     eax, 16
  1427.         and     eax, 0xC03f
  1428. ;     and   eax,0x3f
  1429.         push    eax
  1430.         and     eax, 0x3f
  1431.         mov     edi, esp
  1432.         add     edi, 4+64-1
  1433.         mov     ecx, eax
  1434.         mov     eax, ebx
  1435.         mov     ebx, 2
  1436. @@:
  1437.         xor     edx, edx
  1438.         call    division_64_bits
  1439.         div     ebx
  1440.         add     dl, 48
  1441.         mov     [edi], dl
  1442.         dec     edi
  1443.         loop    @b
  1444.  
  1445.         pop     eax
  1446.         call    normalize_number
  1447.         call    draw_num_text
  1448.         add     esp, 64
  1449.         popad
  1450.         ret
  1451. .no_display_binnum:
  1452.  
  1453.         add     esp, 64
  1454.         popad
  1455.         ret
  1456.  
  1457. normalize_number:
  1458.         test    ah, 0x80
  1459.         jz      .continue
  1460.         mov     ecx, 48
  1461.         and     eax, 0x3f
  1462. @@:
  1463.         inc     edi
  1464.         cmp     [edi], cl
  1465.         jne     .continue
  1466.         dec     eax
  1467.         cmp     eax, 1
  1468.         ja      @r
  1469.         mov     al, 1
  1470. .continue:
  1471.         and     eax, 0x3f
  1472.         ret
  1473.  
  1474. division_64_bits:
  1475.         test    [esp+1+4], byte 0x40
  1476.         jz      .continue
  1477.         push    eax
  1478.         mov     eax, ebp
  1479.         div     ebx
  1480.         mov     ebp, eax
  1481.         pop     eax
  1482. .continue:
  1483.         ret
  1484.  
  1485. draw_num_text:
  1486.         mov     esi, eax
  1487.         mov     edx, 64+4
  1488.         sub     edx, eax
  1489.         add     edx, esp
  1490.         mov     ebx, [esp+64+32-8+4]
  1491. ; add window start x & y
  1492.  
  1493.         mov     ecx, [current_slot]
  1494.         mov     ecx, [ecx + APPDATA.window]
  1495.  
  1496.         mov     eax, [ecx + WDATA.box.left]
  1497.         add     eax, [ecx + WDATA.clientbox.left]
  1498.         shl     eax, 16
  1499.         add     eax, [ecx + WDATA.box.top]
  1500.         add     eax, [ecx + WDATA.clientbox.top]
  1501.         add     ebx, eax
  1502.         mov     ecx, [esp+64+32-12+4]
  1503.         mov     eax, [esp+64+8]         ; background color (if given)
  1504.         mov     edi, [esp+64+4]
  1505.         and     ecx, 5FFFFFFFh
  1506.         bt      ecx, 27
  1507.         jnc     @f
  1508.         mov     edi, eax
  1509. @@:
  1510.         jmp     dtext
  1511. ;-----------------------------------------------------------------------------
  1512. align 4
  1513. sys_setup:
  1514. ;  1 = not used
  1515. ;  2 = keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1516. ;  3 = not used
  1517. ;  4 = not used
  1518. ;  5 = system language, 1eng 2fi 3ger 4rus
  1519. ;  6 = not used
  1520. ;  7 = not used
  1521. ;  8 = not used
  1522. ;  9 = not used
  1523. ; 10 = not used
  1524. ; 11 = enable lba read
  1525. ; 12 = enable pci access
  1526. ;-----------------------------------------------------------------------------
  1527.         and     [esp + SYSCALL_STACK.eax], 0
  1528. ; F.21.2 - set keyboard layout
  1529.         sub     ebx, 2
  1530.         jnz     @f
  1531.  
  1532.         mov     eax, edx
  1533. ; 1 = normal layout
  1534.         dec     ecx
  1535.         jnz     .shift
  1536.  
  1537.         mov     ebx, keymap
  1538.         mov     ecx, 128
  1539.         call    memmove
  1540.         ret
  1541. ;--------------------------------------
  1542. .shift:
  1543. ; 2 = layout at pressed Shift
  1544.         dec     ecx
  1545.         jnz     .alt
  1546.  
  1547.         mov     ebx, keymap_shift
  1548.         mov     ecx, 128
  1549.         call    memmove
  1550.         ret
  1551. ;--------------------------------------
  1552. .alt:
  1553. ; 3 = layout at pressed Alt
  1554.         dec     ecx
  1555.         jnz     .country
  1556.  
  1557.         mov     ebx, keymap_alt
  1558.         mov     ecx, 128
  1559.         call    memmove
  1560.         ret
  1561. ;--------------------------------------
  1562. .country:
  1563. ; country identifier
  1564.         sub     ecx, 6
  1565.         jnz     .error
  1566.  
  1567.         mov     word [keyboard], dx
  1568.         ret
  1569. ;--------------------------------------
  1570. @@:
  1571. ; F.21.5 - set system language
  1572.         sub     ebx, 3
  1573.         jnz     @f
  1574.  
  1575.         mov     [syslang], ecx
  1576.         ret
  1577. ;--------------------------------------
  1578. @@:
  1579. ; F.21.11 - enable/disable low-level access to HD
  1580.         and     ecx, 1
  1581.         sub     ebx, 6
  1582.         jnz     @f
  1583.  
  1584.         mov     [lba_read_enabled], ecx
  1585.         ret
  1586. ;--------------------------------------
  1587. @@:
  1588. ; F.21.12 - enable/disable low-level access to PCI
  1589.         dec     ebx
  1590.         jnz     .error
  1591.  
  1592.         mov     [pci_access_enabled], ecx
  1593.         ret
  1594. ;--------------------------------------
  1595. .error:
  1596.         or      [esp + SYSCALL_STACK.eax], -1
  1597.         ret
  1598. ;-----------------------------------------------------------------------------
  1599. align 4
  1600. sys_getsetup:
  1601. ;  1 = not used
  1602. ;  2 = keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1603. ;  3 = not used
  1604. ;  4 = not used
  1605. ;  5 = system language, 1eng 2fi 3ger 4rus
  1606. ;  6 = not used
  1607. ;  7 = not used
  1608. ;  8 = not used
  1609. ;  9 = get hs timer tic
  1610. ; 10 = not used
  1611. ; 11 = get the state "lba read"
  1612. ; 12 = get the state "pci access"
  1613. ;-----------------------------------------------------------------------------
  1614. ; F.26.2 - get keyboard layout
  1615.         sub     ebx, 2
  1616.         jnz     @f
  1617.  
  1618.         mov     ebx, edx
  1619.         ; if given memory address belongs to kernel then error
  1620.         stdcall is_region_userspace, ebx, 128
  1621.         jnz     .addr_error
  1622. ; 1 = normal layout
  1623.         dec     ecx
  1624.         jnz     .shift
  1625.  
  1626.         mov     eax, keymap
  1627.         mov     ecx, 128
  1628.         call    memmove
  1629.         ret
  1630. ;--------------------------------------
  1631. .shift:
  1632. ; 2 = layout with pressed Shift
  1633.         dec     ecx
  1634.         jnz     .alt
  1635.  
  1636.         mov     eax, keymap_shift
  1637.         mov     ecx, 128
  1638.         call    memmove
  1639.         ret
  1640. ;--------------------------------------
  1641. .alt:
  1642. ; 3 = layout with pressed Alt
  1643.         dec     ecx
  1644.         jne     .country
  1645.  
  1646.         mov     eax, keymap_alt
  1647.         mov     ecx, 128
  1648.         call    memmove
  1649.         ret
  1650. ;--------------------------------------
  1651. .country:
  1652. ; 9 = country identifier
  1653.         sub     ecx, 6
  1654.         jnz     .error
  1655.  
  1656.         movzx   eax, word [keyboard]
  1657.         mov     [esp + SYSCALL_STACK.eax], eax
  1658.         ret
  1659.  
  1660. .addr_error:    ; if given memory address is illegal
  1661.         or      [esp + SYSCALL_STACK.eax], -1
  1662.         ret
  1663. ;--------------------------------------
  1664. @@:
  1665. ; F.26.5 - get system language
  1666.         sub     ebx, 3
  1667.         jnz     @f
  1668.  
  1669.         mov     eax, [syslang]
  1670.         mov     [esp + SYSCALL_STACK.eax], eax
  1671.         ret
  1672. ;--------------------------------------
  1673. @@:
  1674. ; F.26.9 - get the value of the time counter
  1675.         sub     ebx, 4
  1676.         jnz     @f
  1677.  
  1678.         mov     eax, [timer_ticks]
  1679.         mov     [esp + SYSCALL_STACK.eax], eax
  1680.         ret
  1681. ;--------------------------------------
  1682. @@:
  1683. ; F.26.10 - get the time from kernel launch in nanoseconds
  1684.         dec     ebx
  1685.         jnz     @f
  1686.  
  1687.         call    get_clock_ns
  1688.         mov     [esp + SYSCALL_STACK.edx], edx
  1689.         mov     [esp + SYSCALL_STACK.eax], eax
  1690.         ret
  1691. ;--------------------------------------
  1692. @@:
  1693. ; F.26.11 - Find out whether low-level HD access is enabled
  1694.         dec     ebx
  1695.         jnz     @f
  1696.  
  1697.         mov     eax, [lba_read_enabled]
  1698.         mov     [esp + SYSCALL_STACK.eax], eax
  1699.         ret
  1700. ;--------------------------------------
  1701. @@:
  1702. ; F.26.12 - Find out whether low-level PCI access is enabled
  1703.         dec     ebx
  1704.         jnz     .error
  1705.  
  1706.         mov     eax, [pci_access_enabled]
  1707.         mov     [esp + SYSCALL_STACK.eax], eax
  1708.         ret
  1709. ;--------------------------------------
  1710. .error:
  1711.         or      [esp + SYSCALL_STACK.eax], -1
  1712.         ret
  1713. ;-----------------------------------------------------------------------------
  1714. get_timer_ticks:
  1715.         mov     eax, [timer_ticks]
  1716.         ret
  1717. ;-----------------------------------------------------------------------------
  1718. sys_end:
  1719. ; restore default cursor before killing
  1720.         pusha
  1721.         mov     ecx, [current_slot]
  1722.         mov     ecx, [ecx + APPDATA.window]
  1723.         call    restore_default_cursor_before_killing
  1724.         popa
  1725. ;--------------------------------------
  1726. ; kill all sockets this process owns
  1727.         pusha
  1728.         mov     edx, [current_slot]
  1729.         mov     edx, [edx + APPDATA.tid]
  1730.         call    socket_process_end
  1731.         popa
  1732. ;--------------------------------------
  1733.         mov     ecx, [current_slot]
  1734.         mov     eax, [ecx + APPDATA.tls_base]
  1735.         test    eax, eax
  1736.         jz      @F
  1737.  
  1738.         stdcall user_free, eax
  1739. @@:
  1740.  
  1741.         mov     eax, [current_slot]
  1742.         mov     [eax + APPDATA.state], TSTATE_ZOMBIE
  1743.         call    wakeup_osloop
  1744.  
  1745. .waitterm:            ; wait here for termination
  1746.         call    change_task
  1747.         jmp     .waitterm
  1748. ;------------------------------------------------------------------------------
  1749. align 4
  1750. ; ecx - ptr WDATA
  1751. restore_default_cursor_before_killing:
  1752.         pushfd
  1753.         cli
  1754.         mov     eax, [def_cursor]
  1755.         mov     [ecx + WDATA.cursor], eax
  1756.  
  1757.         movzx   eax, word [MOUSE_Y]
  1758.         movzx   ebx, word [MOUSE_X]
  1759.         mov     eax, [d_width_calc_area + eax*4]
  1760.  
  1761.         add     eax, [_display.win_map]
  1762.         movzx   edx, byte [ebx + eax]
  1763.         shl     edx, BSF sizeof.WDATA
  1764.         mov     esi, [window_data + edx + WDATA.cursor]
  1765.  
  1766.         cmp     esi, [current_cursor]
  1767.         je      @f
  1768.  
  1769.         cmp     [_display.select_cursor], 0
  1770.         jz      @f
  1771.  
  1772.         stdcall [_display.select_cursor], esi
  1773.         mov     [current_cursor], esi
  1774. @@:
  1775.         mov     [redrawmouse_unconditional], 1
  1776.         call    wakeup_osloop
  1777.         popfd
  1778.         ret
  1779. ;------------------------------------------------------------------------------
  1780. iglobal
  1781. align 4
  1782. sys_system_table:
  1783.         dd      sysfn_deactivate        ; 1 = deactivate window
  1784.         dd      sysfn_terminate         ; 2 = terminate thread
  1785.         dd      sysfn_activate          ; 3 = activate window
  1786.         dd      sysfn_getidletime       ; 4 = get idle time
  1787.         dd      sysfn_getcpuclock       ; 5 = get cpu clock
  1788.         dd      sysfn_saveramdisk       ; 6 = save ramdisk
  1789.         dd      sysfn_getactive         ; 7 = get active window
  1790.         dd      sysfn_sound_flag        ; 8 = get/set sound_flag
  1791.         dd      sysfn_shutdown          ; 9 = shutdown with parameter
  1792.         dd      sysfn_minimize          ; 10 = minimize window
  1793.         dd      sysfn_getdiskinfo       ; 11 = get disk subsystem info
  1794.         dd      undefined_syscall       ; 12 = get last pressed key. function removed. sysfn_lastkey
  1795.         dd      sysfn_getversion        ; 13 = get kernel version
  1796.         dd      sysfn_waitretrace       ; 14 = wait retrace
  1797.         dd      sysfn_centermouse       ; 15 = center mouse cursor
  1798.         dd      sysfn_getfreemem        ; 16 = get free memory size
  1799.         dd      sysfn_getallmem         ; 17 = get total memory size
  1800.         dd      sysfn_terminate2        ; 18 = terminate thread using PID
  1801.                                         ;                 instead of slot
  1802.         dd      sysfn_mouse_acceleration; 19 = set/get mouse acceleration
  1803.         dd      sysfn_meminfo           ; 20 = get extended memory info
  1804.         dd      sysfn_pid_to_slot       ; 21 = get slot number for pid
  1805.         dd      sysfn_min_rest_window   ; 22 = minimize and restore any window
  1806.         dd      sysfn_min_windows       ; 23 = minimize all windows
  1807.         dd      sysfn_set_screen_sizes  ; 24 = set screen sizes for Vesa
  1808.  
  1809.         dd      sysfn_zmodif            ; 25 = get/set window z modifier  ;Fantomer
  1810. sysfn_num = ($ - sys_system_table)/4
  1811. endg
  1812. ;------------------------------------------------------------------------------
  1813. sys_system:
  1814.         dec     ebx
  1815.         cmp     ebx, sysfn_num
  1816.         jae     @f
  1817.         jmp     dword [sys_system_table + ebx*4]
  1818. @@:
  1819.         ret
  1820. ;------------------------------------------------------------------------------
  1821. sysfn_shutdown:          ; 18.9 = system shutdown
  1822.         cmp     ecx, SYSTEM_SHUTDOWN
  1823.         jl      .exit_for_anyone
  1824.         cmp     ecx, SYSTEM_RESTART
  1825.         jg      .exit_for_anyone
  1826.         mov     [BOOT.shutdown_type], cl
  1827.  
  1828.         mov     eax, [thread_count]
  1829.         mov     [SYS_SHUTDOWN], al
  1830.         mov     [shutdown_processes], eax
  1831.         call    wakeup_osloop
  1832.         and     [esp + SYSCALL_STACK.eax], 0
  1833. .exit_for_anyone:
  1834.         ret
  1835.   uglobal
  1836.    shutdown_processes:
  1837.                        dd 0x0
  1838.   endg
  1839. ;------------------------------------------------------------------------------
  1840. ; in: eax -- APPDATA ptr
  1841. ; out: Z/z -- is/not kernel thread
  1842. is_kernel_thread:
  1843.         mov     eax, [eax + APPDATA.process]
  1844.         cmp     eax, [SLOT_BASE + 2*sizeof.APPDATA + APPDATA.process]       ; OS
  1845.         ret
  1846. ;------------------------------------------------------------------------------
  1847. sysfn_terminate:        ; 18.2 = TERMINATE
  1848.         push    ecx
  1849.         cmp     ecx, 2
  1850.         jb      .noprocessterminate
  1851.         mov     edx, [thread_count]
  1852.         cmp     ecx, edx
  1853.         ja      .noprocessterminate
  1854.         mov     eax, [thread_count]
  1855.         shl     ecx, BSF sizeof.APPDATA
  1856.         add     ecx, SLOT_BASE
  1857.  
  1858.         mov     edx, [ecx + APPDATA.tid]
  1859.         cmp     byte [ecx + APPDATA.state], TSTATE_FREE
  1860.         jz      .noprocessterminate
  1861.         push    eax
  1862.         mov     eax, ecx
  1863.         call    is_kernel_thread
  1864.         pop     eax
  1865.         jz      .noprocessterminate
  1866.         push    ecx edx
  1867.         mov     edx, ecx
  1868.         call    request_terminate
  1869.         pop     edx ecx
  1870.         test    eax, eax
  1871.         jz      .noprocessterminate
  1872. ;--------------------------------------
  1873. ; terminate all network sockets it used
  1874.         pusha
  1875.         mov     eax, edx     ;TODO: check function
  1876.         call    socket_process_end
  1877.         popa
  1878. ;--------------------------------------
  1879. ; restore default cursor before killing
  1880.         pusha
  1881.         mov     ecx, [esp+32]
  1882.         shl     ecx, BSF sizeof.WDATA
  1883.         add     ecx, window_data
  1884.         mov     eax, [def_cursor]
  1885.         cmp     [ecx + WDATA.cursor], eax
  1886.         je      @f
  1887.         call    restore_default_cursor_before_killing
  1888. @@:
  1889.         popa
  1890. ;--------------------------------------
  1891.      ;call MEM_Heap_Lock      ;guarantee that process isn't working with heap
  1892.         mov     [ecx + APPDATA.state], TSTATE_ZOMBIE; clear possible i40's
  1893.         call    wakeup_osloop
  1894.      ;call MEM_Heap_UnLock
  1895.  
  1896.         cmp     edx, [application_table_owner]; clear app table stat
  1897.         jne     .noatsc
  1898.         call    unlock_application_table
  1899. .noatsc:
  1900. .noprocessterminate:
  1901.         add     esp, 4
  1902.         ret
  1903. ;------------------------------------------------------------------------------
  1904. sysfn_terminate2:
  1905.         call    lock_application_table
  1906.         mov     eax, ecx
  1907.         call    pid_to_slot
  1908.         test    eax, eax
  1909.         jz      .not_found
  1910.         mov     ecx, eax
  1911.         cli
  1912.         call    sysfn_terminate
  1913.         call    unlock_application_table
  1914.         sti
  1915.         and     [esp + SYSCALL_STACK.eax], 0
  1916.         ret
  1917. .not_found:
  1918.         call    unlock_application_table
  1919.         or      [esp + SYSCALL_STACK.eax], -1
  1920.         ret
  1921. ;------------------------------------------------------------------------------
  1922. sysfn_deactivate:         ; 18.1 = DEACTIVATE WINDOW
  1923.         cmp     ecx, 2
  1924.         jb      .nowindowdeactivate
  1925.         cmp     ecx, [thread_count]
  1926.         ja      .nowindowdeactivate
  1927.  
  1928.         movzx   esi, word [WIN_STACK + ecx*2]
  1929.         cmp     esi, 1
  1930.         je      .nowindowdeactivate ; already deactive
  1931.  
  1932.         mov     edi, ecx
  1933.         shl     edi, BSF sizeof.WDATA
  1934.         add     edi, window_data
  1935.         movzx   esi, word [WIN_STACK + ecx * 2]
  1936.         lea     esi, [WIN_POS + esi * 2]
  1937.         call    window._.window_deactivate
  1938.         call    syscall_display_settings.calculateScreen
  1939.         call    syscall_display_settings.redrawScreen
  1940. .nowindowdeactivate:
  1941.         ret
  1942. ;------------------------------------------------------------------------------
  1943. sysfn_activate:         ; 18.3 = ACTIVATE WINDOW
  1944.         cmp     ecx, 2
  1945.         jb      .nowindowactivate
  1946.         cmp     ecx, [thread_count]
  1947.         ja      .nowindowactivate
  1948. ;-------------------------------------
  1949. @@:
  1950. ; If the window is captured and moved by the user,
  1951. ; then you can't change the position in window stack!!!
  1952.         mov     al, [mouse.active_sys_window.action]
  1953.         and     al, WINDOW_MOVE_AND_RESIZE_FLAGS
  1954.         test    al, al
  1955.         jz      @f
  1956.         call    change_task
  1957.         jmp     @b
  1958. @@:
  1959. ;-------------------------------------
  1960.         mov     [window_minimize], 2; restore window if minimized
  1961.         call    wakeup_osloop
  1962.  
  1963.         movzx   esi, word [WIN_STACK + ecx*2]
  1964.         cmp     esi, [thread_count]
  1965.         je      .nowindowactivate; already active
  1966.  
  1967.         mov     edi, ecx
  1968.         shl     edi, BSF sizeof.WDATA
  1969.         add     edi, window_data
  1970.         movzx   esi, word [WIN_STACK + ecx * 2]
  1971.         lea     esi, [WIN_POS + esi * 2]
  1972.         call    waredraw
  1973. .nowindowactivate:
  1974.         ret
  1975. ;------------------------------------------------------------------------------
  1976. align 4
  1977. sysfn_zmodif:
  1978. ;18,25,1 - get z_modif
  1979. ;18,25,2 - set z_modif
  1980. ;edx = -1(for current task) or TID
  1981. ;esi(for 2) = new value z_modif
  1982. ;return:
  1983. ;1:   eax = z_modif
  1984. ;2: eax=0(fail),1(success) for set z_modif
  1985.  
  1986.         cmp     edx, -1
  1987.         jne     @f
  1988.         mov     edx, [current_slot_idx]
  1989.      @@:
  1990.         cmp     edx, [thread_count]
  1991.         ja      .fail
  1992.         cmp     edx, 1
  1993.         je      .fail
  1994.  
  1995.         mov     eax, edx
  1996.         shl     edx, BSF sizeof.WDATA
  1997.         add     edx, window_data
  1998.  
  1999.         test    [edx + WDATA.fl_wstate], WSTATE_USED
  2000.         jz      .fail
  2001.  
  2002.         cmp     ecx, 1
  2003.         jnz     .set_zmod
  2004.  
  2005.         movzx   eax, [edx + WDATA.z_modif]
  2006.         jmp     .exit
  2007.  
  2008. .set_zmod:
  2009.         cmp     ecx, 2
  2010.         jnz     .fail
  2011.  
  2012.         mov     ebx, esi
  2013.         mov     esi, eax
  2014.  
  2015.         cmp     bl, ZPOS_ALWAYS_TOP
  2016.         jg      .fail
  2017.  
  2018.         mov     [edx + WDATA.z_modif], bl
  2019.  
  2020.         mov     eax, [edx + WDATA.box.left]
  2021.         mov     ebx, [edx + WDATA.box.top]
  2022.         mov     ecx, [edx + WDATA.box.width]
  2023.         mov     edx, [edx + WDATA.box.height]
  2024.         add     ecx, eax
  2025.         add     edx, ebx
  2026.         call    window._.set_screen
  2027.         call    window._.set_top_wnd
  2028.         call    window._.redraw_top_wnd
  2029.  
  2030.         shl     esi, BSF sizeof.WDATA
  2031.         mov     [esi + window_data + WDATA.fl_redraw], WSTATE_REDRAW
  2032.  
  2033.  
  2034.         mov     eax, 1
  2035.         jmp     .exit
  2036. .fail:
  2037.         xor     eax, eax
  2038. .exit:
  2039.         mov     [esp + SYSCALL_STACK.eax], eax
  2040.         ret
  2041.  
  2042. ;------------------------------------------------------------------------------
  2043. sysfn_getidletime:              ; 18.4 = GET IDLETIME
  2044.         mov     eax, [SLOT_BASE + sizeof.APPDATA + APPDATA.cpu_usage]
  2045.         mov     [esp + SYSCALL_STACK.eax], eax
  2046.         ret
  2047. ;------------------------------------------------------------------------------
  2048. sysfn_getcpuclock:              ; 18.5 = GET TSC/SEC
  2049.         mov     eax, dword [cpu_freq]
  2050.         mov     [esp + SYSCALL_STACK.eax], eax
  2051.         ret
  2052. ;------------------------------------------------------------------------------
  2053. get_cpu_freq:
  2054.         mov     eax, dword [cpu_freq]
  2055.         mov     edx, dword [cpu_freq+4]
  2056.         ret
  2057. ;  SAVE ramdisk to /hd/1/kolibri.img
  2058. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2059.    include 'blkdev/rdsave.inc'
  2060. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2061. ;------------------------------------------------------------------------------
  2062. align 4
  2063. sysfn_getactive:        ; 18.7 = get active window
  2064.         mov     eax, [thread_count]
  2065.         movzx   eax, word [WIN_POS + eax*2]
  2066.         mov     [esp + SYSCALL_STACK.eax], eax
  2067.         ret
  2068. ;------------------------------------------------------------------------------
  2069. sysfn_sound_flag:       ; 18.8 = get/set sound_flag
  2070. ;     cmp  ecx,1
  2071.         dec     ecx
  2072.         jnz     .set_flag
  2073.         movzx   eax, byte [sound_flag]; get sound_flag
  2074.         mov     [esp + SYSCALL_STACK.eax], eax
  2075.         ret
  2076. .set_flag:
  2077. ;     cmp  ecx,2
  2078.         dec     ecx
  2079.         jnz     .err
  2080.         xor     byte [sound_flag], 1
  2081. .err:
  2082.         ret
  2083. ;------------------------------------------------------------------------------
  2084. sysfn_minimize:         ; 18.10 = minimize window
  2085.         mov     [window_minimize], 1
  2086.         call    wakeup_osloop
  2087.         ret
  2088. ;------------------------------------------------------------------------------
  2089. align 4
  2090. sysfn_getdiskinfo:      ; 18.11 = get disk info table
  2091.         dec     ecx
  2092.         jnz     .exit
  2093. .small_table:
  2094.         stdcall is_region_userspace, edx, DRIVE_DATA_SIZE
  2095.         jnz     .exit
  2096.         mov     edi, edx
  2097.         mov     esi, DRIVE_DATA
  2098.         mov     ecx, DRIVE_DATA_SIZE ;10
  2099.         cld
  2100.         rep movsb
  2101. .exit:
  2102.         ret
  2103. ;------------------------------------------------------------------------------
  2104. sysfn_getversion:       ; 18.13 = get kernel ID and version
  2105.         ; if given memory address belongs to kernel then error
  2106.         stdcall is_region_userspace, ecx, version_inf.size
  2107.         jnz     .addr_error
  2108.  
  2109.         mov     edi, ecx
  2110.         mov     esi, version_inf
  2111.         mov     ecx, version_inf.size
  2112.         rep movsb
  2113.         ret
  2114. .addr_error:    ; if given memory address is illegal
  2115.         mov     [esp + SYSCALL_STACK.eax], -1
  2116.         ret
  2117. ;------------------------------------------------------------------------------
  2118. sysfn_waitretrace:     ; 18.14 = sys wait retrace
  2119.         ;wait retrace functions
  2120.         mov     edx, 0x3da
  2121. .loop:
  2122.         in      al, dx
  2123.         test    al, 1000b
  2124.         jz      .loop
  2125.         and     [esp + SYSCALL_STACK.eax], 0
  2126.         ret
  2127. ;------------------------------------------------------------------------------
  2128. align 4
  2129. sysfn_centermouse:      ; 18.15 = mouse centered
  2130.         mov     eax, [_display.width]
  2131.         shr     eax, 1
  2132.         mov     [MOUSE_X], ax
  2133.         mov     eax, [_display.height]
  2134.         shr     eax, 1
  2135.         mov     [MOUSE_Y], ax
  2136.         call    wakeup_osloop
  2137.         xor     eax, eax
  2138.         and     [esp + SYSCALL_STACK.eax], eax
  2139.         ret
  2140. ;------------------------------------------------------------------------------
  2141. sysfn_mouse_acceleration:       ; 18.19 = set/get mouse features
  2142.         cmp     ecx, 8
  2143.         jnc     @f
  2144.         jmp     dword [.table + ecx*4]
  2145. .get_mouse_acceleration:
  2146.         xor     eax, eax
  2147.         mov     ax, [mouse_speed_factor]
  2148.         mov     [esp + SYSCALL_STACK.eax], eax
  2149.         ret
  2150. .set_mouse_acceleration:
  2151.         mov     [mouse_speed_factor], dx
  2152.         ret
  2153. .get_mouse_delay:
  2154.         xor     eax, eax
  2155.         mov     al, [mouse_delay]
  2156.         mov     [esp + SYSCALL_STACK.eax], eax
  2157.         ret
  2158. .set_mouse_delay:
  2159.         mov     [mouse_delay], dl
  2160. @@:
  2161.         ret
  2162. .set_pointer_position:
  2163.         cmp     dx, word[_display.height]
  2164.         jae     @b
  2165.         rol     edx, 16
  2166.         cmp     dx, word[_display.width]
  2167.         jae     @b
  2168.         mov     [MOUSE_X], edx
  2169.         mov     [mouse_active], 1
  2170.         jmp     wakeup_osloop
  2171. .set_mouse_button:
  2172.         mov     [BTN_DOWN], edx
  2173.         mov     [mouse_active], 1
  2174.         jmp     wakeup_osloop
  2175. .get_doubleclick_delay:
  2176.         xor     eax, eax
  2177.         mov     al, [mouse_doubleclick_delay]
  2178.         mov     [esp + SYSCALL_STACK.eax], eax
  2179.         ret
  2180. .set_doubleclick_delay:
  2181.         mov     [mouse_doubleclick_delay], dl
  2182.         ret
  2183. align 4
  2184. .table:
  2185. dd      .get_mouse_acceleration
  2186. dd      .set_mouse_acceleration
  2187. dd      .get_mouse_delay
  2188. dd      .set_mouse_delay
  2189. dd      .set_pointer_position
  2190. dd      .set_mouse_button
  2191. dd      .get_doubleclick_delay
  2192. dd      .set_doubleclick_delay
  2193. ;------------------------------------------------------------------------------
  2194. sysfn_getfreemem:
  2195.         mov     eax, [pg_data.pages_free]
  2196.         shl     eax, 2
  2197.         mov     [esp + SYSCALL_STACK.eax], eax
  2198.         ret
  2199. ;------------------------------------------------------------------------------
  2200. sysfn_getallmem:
  2201.         mov     eax, [MEM_AMOUNT]
  2202.         shr     eax, 10
  2203.         mov     [esp + SYSCALL_STACK.eax], eax
  2204.         ret
  2205. ;------------------------------------------------------------------------------
  2206. sysfn_pid_to_slot:
  2207.         mov     eax, ecx
  2208.         call    pid_to_slot
  2209.         mov     [esp + SYSCALL_STACK.eax], eax
  2210.         ret
  2211. ;------------------------------------------------------------------------------
  2212. sysfn_min_rest_window:
  2213.         pushad
  2214.         mov     eax, edx ; ebx - operating
  2215.         shr     ecx, 1
  2216.         jnc     @f
  2217.         call    pid_to_slot
  2218. @@:
  2219.         or      eax, eax ; eax - number of slot
  2220.         jz      .error
  2221.         cmp     eax, max_processes    ; varify maximal slot number
  2222.         ja      .error
  2223.         movzx   eax, word [WIN_STACK + eax*2]
  2224.         shr     ecx, 1
  2225.         jc      .restore
  2226.  ; .minimize:
  2227.         call    minimize_window
  2228.         jmp     .exit
  2229. .restore:
  2230.         call    restore_minimized_window
  2231. .exit:
  2232.         popad
  2233.         xor     eax, eax
  2234.         mov     [esp + SYSCALL_STACK.eax], eax
  2235.         ret
  2236. .error:
  2237.         popad
  2238.         xor     eax, eax
  2239.         dec     eax
  2240.         mov     [esp + SYSCALL_STACK.eax], eax
  2241.         ret
  2242. ;------------------------------------------------------------------------------
  2243. sysfn_min_windows:
  2244.         call    minimize_all_window
  2245.         mov     [esp + SYSCALL_STACK.eax], eax
  2246.         call    change_task
  2247.         ret
  2248. ;------------------------------------------------------------------------------
  2249. sysfn_set_screen_sizes:
  2250.         cmp     [SCR_MODE], word 0x13
  2251.         jbe     .exit
  2252.  
  2253.         cmp     [_display.select_cursor], select_cursor
  2254.         jne     .exit
  2255.  
  2256.         cmp     ecx, [display_width_standard]
  2257.         ja      .exit
  2258.  
  2259.         cmp     edx, [display_height_standard]
  2260.         ja      .exit
  2261.  
  2262.         pushfd
  2263.         cli
  2264.         mov     eax, ecx
  2265.         mov     ecx, [_display.lfb_pitch]
  2266.         mov     [_display.width], eax
  2267.         dec     eax
  2268.         mov     [_display.height], edx
  2269.         dec     edx
  2270. ; eax - new Screen_Max_X
  2271. ; edx - new Screen_Max_Y
  2272.         mov     [do_not_touch_winmap], 1
  2273.         call    set_screen
  2274.         mov     [do_not_touch_winmap], 0
  2275.         popfd
  2276.         call    change_task
  2277. .exit:
  2278.         ret
  2279. ;------------------------------------------------------------------------------
  2280. uglobal
  2281. screen_workarea RECT
  2282. display_width_standard dd 0
  2283. display_height_standard dd 0
  2284. do_not_touch_winmap db 0
  2285. window_minimize db 0
  2286. sound_flag      db 0
  2287.  
  2288. endg
  2289.  
  2290. iglobal
  2291. version_inf:
  2292.         db 0,7,7,0  ; version 0.7.7.0
  2293.         db 0
  2294. .rev    dd __REV__
  2295. .size = $ - version_inf
  2296. endg
  2297. ;------------------------------------------------------------------------------
  2298. align 4
  2299. sys_cachetodiskette:
  2300.         cmp     ebx, 1
  2301.         jb      .no_floppy_save
  2302.         cmp     ebx, 2
  2303.         ja      .no_floppy_save
  2304.         call    save_image
  2305.         mov     [esp + SYSCALL_STACK.eax], eax
  2306.         ret
  2307. .no_floppy_save:
  2308.         mov     [esp + SYSCALL_STACK.eax], 1
  2309.         ret
  2310. ;------------------------------------------------------------------------------
  2311. align 4
  2312. sys_cpuusage:
  2313.  
  2314. ;  RETURN: process_information structure
  2315. ;
  2316.         ; if given memory address belongs to kernel then error
  2317.         stdcall is_region_userspace, ebx, sizeof.process_information
  2318.         jnz     .addr_error
  2319.  
  2320.         cmp     ecx, -1 ; who am I ?
  2321.         jne     .no_who_am_i
  2322.         mov     ecx, [current_slot_idx]
  2323. .no_who_am_i:
  2324.         jecxz   .empty_slot
  2325.         cmp     ecx, max_processes
  2326.         ja      .empty_slot
  2327.         mov     edx, ecx
  2328.         shl     edx, BSF sizeof.APPDATA
  2329.         cmp     [SLOT_BASE+edx+APPDATA.state], TSTATE_FREE
  2330.         jnz     .thread_found
  2331. .empty_slot:
  2332.         ; zero buffer for an empty slot
  2333.         push    edi
  2334.         xor     eax, eax
  2335.         mov     edi, ebx
  2336.         movi    ecx, sizeof.process_information
  2337.         rep stosb
  2338.         pop     edi
  2339.         jmp     .nofillbuf
  2340. .thread_found:
  2341. ; +4: word: position of the window of thread in the window stack
  2342.         mov     ax, [WIN_STACK + ecx * 2]
  2343.         mov     [ebx+process_information.window_stack_position], ax
  2344. ; +6: word: number of the thread slot, which window has in the window stack
  2345. ;           position ecx (has no relation to the specific thread)
  2346.         mov     ax, [WIN_POS + ecx * 2]
  2347.         mov     [ebx+process_information.window_stack_value], ax
  2348.  
  2349.         shl     ecx, BSF sizeof.APPDATA
  2350.  
  2351. ; +0: dword: memory usage
  2352.         mov     eax, [SLOT_BASE + ecx + APPDATA.cpu_usage]
  2353.         mov     [ebx+process_information.cpu_usage], eax
  2354. ; +10: 11 bytes: name of the process
  2355.         push    ecx
  2356.         lea     eax, [SLOT_BASE + ecx + APPDATA.app_name]
  2357.         add     ebx, process_information.process_name
  2358.         mov     ecx, 11
  2359.         call    memmove
  2360.         pop     ecx
  2361.  
  2362. ; +22: address of the process in memory
  2363. ; +26: size of used memory - 1
  2364.         push    edi
  2365.         lea     edi, [ebx+12]
  2366.         xor     eax, eax
  2367.         mov     edx, 0x100000*16
  2368.         cmp     ecx, 1 shl BSF sizeof.APPDATA
  2369.         je      .os_mem
  2370.         mov     edx, [SLOT_BASE + ecx + APPDATA.process]
  2371.         mov     edx, [edx + PROC.mem_used]
  2372.         mov     eax, std_application_base_address
  2373. .os_mem:
  2374.         stosd
  2375.         lea     eax, [edx-1]
  2376.         stosd
  2377.  
  2378.         mov     edx, [SLOT_BASE + ecx + APPDATA.window]
  2379.  
  2380. ; +30: PID/TID
  2381.         mov     eax, [SLOT_BASE + ecx + APPDATA.tid]
  2382.         stosd
  2383.  
  2384.     ; window position and size
  2385.         push    esi
  2386.         lea     esi, [edx + WDATA.box]
  2387.         movsd
  2388.         movsd
  2389.         movsd
  2390.         movsd
  2391.  
  2392.     ; Process state (+50)
  2393.         movzx   eax, byte [SLOT_BASE + ecx + APPDATA.state]
  2394.         stosd
  2395.  
  2396.     ; Window client area box
  2397.         lea     esi, [edx + WDATA.clientbox]
  2398.         movsd
  2399.         movsd
  2400.         movsd
  2401.         movsd
  2402.  
  2403.     ; Window state
  2404.         mov     al, [edx + WDATA.fl_wstate]
  2405.         stosb
  2406.  
  2407.     ; Event mask (+71)
  2408.         mov     eax, [SLOT_BASE + ecx + APPDATA.event_mask]
  2409.         stosd
  2410.  
  2411.     ; Keyboard mode (+75)
  2412.         mov     al, [SLOT_BASE + ecx + APPDATA.keyboard_mode]
  2413.         stosb
  2414.  
  2415.         pop     esi
  2416.         pop     edi
  2417.  
  2418. .nofillbuf:
  2419.     ; return number of processes
  2420.  
  2421.         mov     eax, [thread_count]
  2422.         mov     [esp + SYSCALL_STACK.eax], eax
  2423.         ret
  2424.  
  2425. .addr_error:    ; if given memory address is illegal
  2426.         mov     [esp + SYSCALL_STACK.eax], -1
  2427.         ret
  2428.  
  2429.  
  2430. ; redraw status
  2431. align 4
  2432. sys_redrawstat:
  2433.         cmp     ebx, 1
  2434.         jne     .no_widgets_away
  2435.         ; buttons away
  2436.         mov     ecx, [current_slot_idx]
  2437. .sys_newba2:
  2438.         mov     edi, [BTN_ADDR]
  2439.         cmp     [edi], dword 0  ; empty button list ?
  2440.         je      .end_of_buttons_away
  2441.         movzx   ebx, word [edi]
  2442.         inc     ebx
  2443.         mov     eax, edi
  2444. .sys_newba:
  2445.         dec     ebx
  2446.         jz      .end_of_buttons_away
  2447.  
  2448.         add     eax, 0x10
  2449.         cmp     cx, [eax]
  2450.         jnz     .sys_newba
  2451.  
  2452.         push    eax ebx ecx
  2453.         mov     ecx, ebx
  2454.         inc     ecx
  2455.         shl     ecx, 4
  2456.         mov     ebx, eax
  2457.         add     eax, 0x10
  2458.         call    memmove
  2459.         dec     dword [edi]
  2460.         pop     ecx ebx eax
  2461.  
  2462.         jmp     .sys_newba2
  2463.  
  2464. .end_of_buttons_away:
  2465.         ret
  2466.  
  2467. .no_widgets_away:
  2468.  
  2469.         cmp     ebx, 2
  2470.         jnz     .srl1
  2471.  
  2472.         mov     edx, [current_slot]      ; return whole screen draw area for this app
  2473.         mov     edx, [edx + APPDATA.window]
  2474.         mov     [edx + WDATA.draw_data.left], 0
  2475.         mov     [edx + WDATA.draw_data.top], 0
  2476.         mov     eax, [_display.width]
  2477.         dec     eax
  2478.         mov     [edx + WDATA.draw_data.right], eax
  2479.         mov     eax, [_display.height]
  2480.         dec     eax
  2481.         mov     [edx + WDATA.draw_data.bottom], eax
  2482.  
  2483. .srl1:
  2484.         ret
  2485.  
  2486. ;ok - 100% work
  2487. ;nt - not tested
  2488. ;---------------------------------------------------------------------------------------------
  2489. ;eax
  2490. ;0 - task switch counter. Ret switch counter in eax. Block. ok.
  2491. ;1 - change task. Ret nothing. Block. ok.
  2492. ;2 - performance control
  2493. ; ebx
  2494. ; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
  2495. ; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
  2496. ; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
  2497. ; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
  2498. ; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
  2499. ;eax
  2500. ;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  2501. ;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  2502. ;---------------------------------------------------------------------------------------------
  2503. iglobal
  2504. align 4
  2505. sheduler:
  2506.         dd      sys_sheduler.00
  2507.         dd      change_task
  2508.         dd      sys_sheduler.02
  2509.         dd      sys_sheduler.03
  2510.         dd      sys_sheduler.04
  2511. endg
  2512. sys_sheduler:
  2513. ;rewritten by <Lrz>  29.12.2009
  2514.         jmp     dword [sheduler + ebx*4]
  2515. ;.shed_counter:
  2516. .00:
  2517.         mov     eax, [context_counter]
  2518.         mov     [esp + SYSCALL_STACK.eax], eax
  2519.         ret
  2520.  
  2521. .02:
  2522. ;.perf_control:
  2523.         inc     ebx                     ;before ebx=2, ebx=3
  2524.         cmp     ebx, ecx                ;if ecx=3, ebx=3
  2525.         jz      cache_disable
  2526.  
  2527.         dec     ebx                     ;ebx=2
  2528.         cmp     ebx, ecx                ;
  2529.         jz      cache_enable            ;if ecx=2 and ebx=2
  2530.  
  2531.         dec     ebx                     ;ebx=1
  2532.         cmp     ebx, ecx
  2533.         jz      is_cache_enabled        ;if ecx=1 and ebx=1
  2534.  
  2535.         dec     ebx
  2536.         test    ebx, ecx                ;ebx=0 and ecx=0
  2537.         jz      modify_pce              ;if ecx=0
  2538.  
  2539.         ret
  2540.  
  2541. .03:
  2542. ;.rdmsr_instr:
  2543. ;now counter in ecx
  2544. ;(edx:eax) esi:edi => edx:esi
  2545.         mov     eax, esi
  2546.         mov     ecx, edx
  2547.         rdmsr
  2548.         mov     [esp + SYSCALL_STACK.eax], eax
  2549.         mov     [esp + SYSCALL_STACK.ebx], edx           ;ret in ebx?
  2550.         ret
  2551.  
  2552. .04:
  2553. ;.wrmsr_instr:
  2554. ;now counter in ecx
  2555. ;(edx:eax) esi:edi => edx:esi
  2556.         ; Fast Call MSR can't be destroy
  2557.         ; Но MSR_AMD_EFER можно изменять, т.к. в этом регистре лиш
  2558.         ; включаются/выключаются расширенные возможности
  2559.         cmp     edx, MSR_SYSENTER_CS
  2560.         je      @f
  2561.         cmp     edx, MSR_SYSENTER_ESP
  2562.         je      @f
  2563.         cmp     edx, MSR_SYSENTER_EIP
  2564.         je      @f
  2565.         cmp     edx, MSR_AMD_STAR
  2566.         je      @f
  2567.  
  2568.         mov     eax, esi
  2569.         mov     ecx, edx
  2570.         wrmsr
  2571.         ; mov   [esp + 32], eax
  2572.         ; mov   [esp + 20], edx ;ret in ebx?
  2573. @@:
  2574.         ret
  2575.  
  2576. cache_disable:
  2577.         mov     eax, cr0
  2578.         or      eax, 01100000_00000000_00000000_00000000b
  2579.         mov     cr0, eax
  2580.         wbinvd  ;set MESI
  2581.         ret
  2582.  
  2583. cache_enable:
  2584.         mov     eax, cr0
  2585.         and     eax, 10011111_11111111_11111111_11111111b
  2586.         mov     cr0, eax
  2587.         ret
  2588.  
  2589. is_cache_enabled:
  2590.         mov     eax, cr0
  2591.         and     eax, 01100000_00000000_00000000_00000000b
  2592.         mov     [esp + SYSCALL_STACK.eax], eax
  2593.         ret
  2594.  
  2595. modify_pce:
  2596.         mov     eax, cr4
  2597. ;       mov ebx,0
  2598. ;       or  bx,100000000b ;pce
  2599. ;       xor eax,ebx ;invert pce
  2600.         bts     eax, 8;pce=cr4[8]
  2601.         mov     cr4, eax
  2602.         mov     [esp + SYSCALL_STACK.eax], eax
  2603.         ret
  2604. ;---------------------------------------------------------------------------------------------
  2605.  
  2606.  
  2607. iglobal
  2608.   cpustring db 'CPU',0
  2609. endg
  2610.  
  2611. uglobal
  2612. background_defined    db    0    ; diamond, 11.04.2006
  2613. endg
  2614. ;-----------------------------------------------------------------------------
  2615. align 4
  2616. checkmisc:
  2617.         cmp     [ctrl_alt_del], 1
  2618.         jne     .nocpustart
  2619.  
  2620.         mov     ebp, cpustring
  2621.         call    fs_execute_from_sysdir
  2622.  
  2623.         mov     [ctrl_alt_del], 0
  2624. ;--------------------------------------
  2625. align 4
  2626. .nocpustart:
  2627.         cmp     [mouse_active], 1
  2628.         jne     .mouse_not_active
  2629.         mov     [mouse_active], 0
  2630.  
  2631.         xor     edi, edi
  2632.         mov     ebx, window_data
  2633.  
  2634.         mov     ecx, [thread_count]
  2635.         movzx   eax, word [WIN_POS + ecx*2]     ; active window
  2636.         shl     eax, BSF sizeof.APPDATA
  2637.         push    eax
  2638.  
  2639.         movzx   eax, word [MOUSE_X]
  2640.         movzx   edx, word [MOUSE_Y]
  2641. ;--------------------------------------
  2642. align 4
  2643. .set_mouse_event:
  2644.         add     edi, sizeof.APPDATA
  2645.         add     ebx, sizeof.WDATA
  2646.         test    [SLOT_BASE + edi + APPDATA.event_mask], EVM_MOUSE_FILTER
  2647.         jz      .pos_filter
  2648.  
  2649.         cmp     edi, [esp]                      ; skip if filtration active
  2650.         jne     .skip
  2651. ;--------------------------------------
  2652. align 4
  2653. .pos_filter:
  2654.         test    [SLOT_BASE + edi + APPDATA.event_mask], EVM_CURSOR_FILTER
  2655.         jz      .set
  2656.  
  2657.         mov     esi, [ebx + WDATA.box.left]
  2658.         cmp     eax, esi
  2659.         jb      .skip
  2660.         add     esi, [ebx + WDATA.box.width]
  2661.         cmp     eax, esi
  2662.         ja      .skip
  2663.  
  2664.         mov     esi, [ebx + WDATA.box.top]
  2665.         cmp     edx, esi
  2666.         jb      .skip
  2667.         add     esi, [ebx + WDATA.box.height]
  2668.         cmp     edx, esi
  2669.         ja      .skip
  2670. ;--------------------------------------
  2671. align 4
  2672. .set:
  2673.         or      [SLOT_BASE + edi + APPDATA.occurred_events], EVENT_MOUSE
  2674. ;--------------------------------------
  2675. align 4
  2676. .skip:
  2677.         loop    .set_mouse_event
  2678.  
  2679.         pop     eax
  2680. ;--------------------------------------
  2681. align 4
  2682. .mouse_not_active:
  2683.         cmp     [REDRAW_BACKGROUND], 0  ; background update ?
  2684.         jz      nobackgr
  2685.  
  2686.         cmp     [background_defined], 0
  2687.         jz      nobackgr
  2688. ;--------------------------------------
  2689. align 4
  2690. backgr:
  2691.         mov     eax, [background_window + WDATA.draw_data.left]
  2692.         shl     eax, 16
  2693.         add     eax, [background_window + WDATA.draw_data.right]
  2694.         mov     [BG_Rect_X_left_right], eax ; [left]*65536 + [right]
  2695.  
  2696.         mov     eax, [background_window + WDATA.draw_data.top]
  2697.         shl     eax, 16
  2698.         add     eax, [background_window + WDATA.draw_data.bottom]
  2699.         mov     [BG_Rect_Y_top_bottom], eax ; [top]*65536 + [bottom]
  2700.  
  2701.         call    drawbackground
  2702. ;        DEBUGF  1, "K : drawbackground\n"
  2703. ;        DEBUGF  1, "K : backg x %x\n",[BG_Rect_X_left_right]
  2704. ;        DEBUGF  1, "K : backg y %x\n",[BG_Rect_Y_top_bottom]
  2705. ;--------- set event 5 start ----------
  2706.         push    ecx edi
  2707.         mov     edi, window_data
  2708.         mov     ecx, [thread_count]
  2709. ;--------------------------------------
  2710. align 4
  2711. set_bgr_event:
  2712.         add     edi, sizeof.WDATA
  2713.         mov     eax, [BG_Rect_X_left_right]
  2714.         mov     edx, [BG_Rect_Y_top_bottom]
  2715.         cmp     [edi + WDATA.draw_bgr_x], 0
  2716.         jz      .set
  2717. .join:
  2718.         cmp     word [edi + WDATA.draw_bgr_x], ax
  2719.         jae     @f
  2720.         mov     word [edi + WDATA.draw_bgr_x], ax
  2721. @@:
  2722.         shr     eax, 16
  2723.         cmp     word [edi + WDATA.draw_bgr_x + 2], ax
  2724.         jbe     @f
  2725.         mov     word [edi + WDATA.draw_bgr_x + 2], ax
  2726. @@:
  2727.         cmp     word [edi + WDATA.draw_bgr_y], dx
  2728.         jae     @f
  2729.         mov     word [edi + WDATA.draw_bgr_y], dx
  2730. @@:
  2731.         shr     edx, 16
  2732.         cmp     word [edi + WDATA.draw_bgr_y+2], dx
  2733.         jbe     @f
  2734.         mov     word [edi + WDATA.draw_bgr_y+2], dx
  2735. @@:
  2736.         jmp     .common
  2737. .set:
  2738.         mov     [edi + WDATA.draw_bgr_x], eax
  2739.         mov     [edi + WDATA.draw_bgr_y], edx
  2740. .common:
  2741.         mov     eax, [edi + WDATA.thread]
  2742.         test    eax, eax
  2743.         jz      @f
  2744.         or      [eax + APPDATA.occurred_events], EVENT_BACKGROUND
  2745. @@:
  2746.         loop    set_bgr_event
  2747.         pop     edi ecx
  2748. ;--------- set event 5 stop -----------
  2749.         dec     [REDRAW_BACKGROUND]     ; got new update request?
  2750.         jnz     backgr
  2751.  
  2752.         xor     eax, eax
  2753.         mov     [background_window + WDATA.draw_data.left], eax
  2754.         mov     [background_window + WDATA.draw_data.top], eax
  2755.         mov     [background_window + WDATA.draw_data.right], eax
  2756.         mov     [background_window + WDATA.draw_data.bottom], eax
  2757. ;--------------------------------------
  2758. align 4
  2759. nobackgr:
  2760. ; system shutdown request
  2761.         cmp     [SYS_SHUTDOWN], byte 0
  2762.         je      noshutdown
  2763.  
  2764.         mov     edx, [shutdown_processes]
  2765.  
  2766.         cmp     [SYS_SHUTDOWN], dl
  2767.         jne     noshutdown
  2768.  
  2769.         lea     ecx, [edx-1]
  2770.         mov     edx, SLOT_BASE + sizeof.APPDATA*2 ;OS_BASE+0x3040
  2771.         jecxz   no_mark_system_shutdown
  2772. ;--------------------------------------
  2773. align 4
  2774. markz:
  2775.         push    ecx edx
  2776.         cmp     [edx + APPDATA.state], TSTATE_FREE
  2777.         jz      .nokill
  2778.         cmp     [edx + APPDATA.process], sys_proc
  2779.         jz      .nokill
  2780.         call    request_terminate
  2781.         jmp     .common
  2782. .nokill:
  2783.         dec     byte [SYS_SHUTDOWN]
  2784.         xor     eax, eax
  2785. .common:
  2786.         pop     edx ecx
  2787.         test    eax, eax
  2788.         jz      @f
  2789.         mov     [edx + APPDATA.state], TSTATE_ZOMBIE
  2790. @@:
  2791.         add     edx, sizeof.APPDATA
  2792.         loop    markz
  2793.         call    wakeup_osloop
  2794. ;--------------------------------------
  2795. align 4
  2796. @@:
  2797. no_mark_system_shutdown:
  2798.         dec     byte [SYS_SHUTDOWN]
  2799.         je      system_shutdown
  2800. ;--------------------------------------
  2801. align 4
  2802. noshutdown:
  2803.         mov     eax, [thread_count]           ; termination
  2804.         mov     ebx, SLOT_BASE + sizeof.APPDATA + APPDATA.state
  2805.         mov     esi, 1
  2806. ;--------------------------------------
  2807. align 4
  2808. newct:
  2809.         mov     cl, [ebx]
  2810.         cmp     cl, TSTATE_ZOMBIE
  2811.         jz      .terminate
  2812.  
  2813.         cmp     cl, TSTATE_TERMINATING
  2814.         jnz     .noterminate
  2815. .terminate:
  2816.         pushad
  2817.         push    esi
  2818.         mov     ecx, dword[ebx - APPDATA.state + APPDATA.window]
  2819.         call    restore_default_cursor_before_killing
  2820.  
  2821.         pop     esi
  2822.         call    terminate
  2823.         popad
  2824.         cmp     byte [SYS_SHUTDOWN], 0
  2825.         jz      .noterminate
  2826.         dec     byte [SYS_SHUTDOWN]
  2827.         je      system_shutdown
  2828.  
  2829. .noterminate:
  2830.         add     ebx, sizeof.APPDATA
  2831.         inc     esi
  2832.         dec     eax
  2833.         jnz     newct
  2834.         ret
  2835. ;-----------------------------------------------------------------------------
  2836. align 4
  2837. ; eax - ptr to WDATA
  2838. redrawscreen:
  2839. ; eax , if process window_data base is eax, do not set flag/limits
  2840.  
  2841.         pushad
  2842.         push    eax
  2843.  
  2844. ;;;         mov   ebx,2
  2845. ;;;         call  delay_hs
  2846.  
  2847.          ;mov   ecx,0               ; redraw flags for apps
  2848.         xor     ecx, ecx
  2849. ;--------------------------------------
  2850. align 4
  2851. newdw2:
  2852.         inc     ecx
  2853.         push    ecx
  2854.  
  2855.         mov     eax, ecx
  2856.         shl     eax, BSF sizeof.WDATA
  2857.         add     eax, window_data
  2858.  
  2859.         cmp     eax, [esp+4]
  2860.         je      not_this_task
  2861.                                    ; check if window in redraw area
  2862.         mov     edi, eax
  2863.  
  2864.         cmp     ecx, 1             ; limit for background
  2865.         jz      bgli
  2866.  
  2867.         mov     eax, [esp+4]        ;if upper in z-position - no redraw
  2868.         test    eax, eax
  2869.         jz      @f
  2870.         mov     al, [eax + WDATA.z_modif]
  2871.         cmp     [edi + WDATA.z_modif], al
  2872.         jg      ricino
  2873.       @@:
  2874.  
  2875.         mov     eax, [edi + WDATA.box.left]
  2876.         mov     ebx, [edi + WDATA.box.top]
  2877.  
  2878.         cmp     ebx, [draw_limits.bottom] ; ecx = area y end     ebx = window y start
  2879.         jae     ricino
  2880.  
  2881.         cmp     eax, [draw_limits.right] ; ecx = area x end     eax = window x start
  2882.         jae     ricino
  2883.  
  2884.         mov     eax, [edi + WDATA.box.left]
  2885.         mov     ebx, [edi + WDATA.box.top]
  2886.         mov     ecx, [edi + WDATA.box.width]
  2887.         mov     edx, [edi + WDATA.box.height]
  2888.         add     ecx, eax
  2889.         add     edx, ebx
  2890.  
  2891.         mov     eax, [draw_limits.top]  ; eax = area y start     edx = window y end
  2892.         cmp     edx, eax
  2893.         jb      ricino
  2894.  
  2895.         mov     eax, [draw_limits.left]  ; eax = area x start     ecx = window x end
  2896.         cmp     ecx, eax
  2897.         jb      ricino
  2898. ;--------------------------------------
  2899. align 4
  2900. bgli:
  2901.         cmp     dword[esp], 1  ; check index in window_data array, 1 - idle
  2902.         jnz     .az
  2903.  
  2904.         cmp     [REDRAW_BACKGROUND], 0
  2905.         jz      .az
  2906.  
  2907.         mov     dl, 0
  2908.         mov     ebx, [draw_limits.left]
  2909.         cmp     ebx, [edi + WDATA.draw_data.left]
  2910.         jae     @f
  2911.  
  2912.         mov     [edi + WDATA.draw_data.left], ebx
  2913.         mov     dl, 1
  2914. ;--------------------------------------
  2915. align 4
  2916. @@:
  2917.         mov     ebx, [draw_limits.top]
  2918.         cmp     ebx, [edi + WDATA.draw_data.top]
  2919.         jae     @f
  2920.  
  2921.         mov     [edi + WDATA.draw_data.top], ebx
  2922.         mov     dl, 1
  2923. ;--------------------------------------
  2924. align 4
  2925. @@:
  2926.         mov     ebx, [draw_limits.right]
  2927.         cmp     ebx, [edi + WDATA.draw_data.right]
  2928.         jbe     @f
  2929.  
  2930.         mov     [edi + WDATA.draw_data.right], ebx
  2931.         mov     dl, 1
  2932. ;--------------------------------------
  2933. align 4
  2934. @@:
  2935.         mov     ebx, [draw_limits.bottom]
  2936.         cmp     ebx, [edi + WDATA.draw_data.bottom]
  2937.         jbe     @f
  2938.  
  2939.         mov     [edi + WDATA.draw_data.bottom], ebx
  2940.         mov     dl, 1
  2941. ;--------------------------------------
  2942. align 4
  2943. @@:
  2944.         add     [REDRAW_BACKGROUND], dl
  2945.         call    wakeup_osloop
  2946.         jmp     newdw8
  2947. ;--------------------------------------
  2948. align 4
  2949. .az:
  2950.         mov     ebx, [draw_limits.left]        ; set limits
  2951.         mov     [edi + WDATA.draw_data.left], ebx
  2952.         mov     ebx, [draw_limits.top]
  2953.         mov     [edi + WDATA.draw_data.top], ebx
  2954.         mov     ebx, [draw_limits.right]
  2955.         mov     [edi + WDATA.draw_data.right], ebx
  2956.         mov     ebx, [draw_limits.bottom]
  2957.         mov     [edi + WDATA.draw_data.bottom], ebx
  2958.  
  2959.         cmp     dword [esp], 1  ; check idle thread
  2960.         jne     nobgrd
  2961.         inc     [REDRAW_BACKGROUND]
  2962.         call    wakeup_osloop
  2963. ;--------------------------------------
  2964. align 4
  2965. newdw8:
  2966. nobgrd:
  2967. ;--------------------------------------
  2968.         push    edi ebp
  2969.         mov     edi, [esp+8]
  2970.         cmp     edi, 1
  2971.         je      .found
  2972.  
  2973.         mov     eax, [draw_limits.left]
  2974.         mov     ebx, [draw_limits.top]
  2975.         mov     ecx, [draw_limits.right]
  2976.         sub     ecx, eax
  2977.         test    ecx, ecx
  2978.         jz      .not_found
  2979.  
  2980.         mov     edx, [draw_limits.bottom]
  2981.         sub     edx, ebx
  2982.         test    edx, edx
  2983.         jz      .not_found
  2984.  
  2985. ; eax - x, ebx - y
  2986. ; ecx - size x, edx - size y
  2987.         add     ebx, edx
  2988. ;--------------------------------------
  2989. align 4
  2990. .start_y:
  2991.         push    ecx
  2992. ;--------------------------------------
  2993. align 4
  2994. .start_x:
  2995.         add     eax, ecx
  2996.         mov     ebp, [d_width_calc_area + ebx*4]
  2997.         add     ebp, [_display.win_map]
  2998.         movzx   ebp, byte[eax+ebp] ; get value for current point
  2999.         cmp     ebp, edi
  3000.         jne     @f
  3001.  
  3002.         pop     ecx
  3003.         jmp     .found
  3004. ;--------------------------------------
  3005. align 4
  3006. @@:
  3007.         sub     eax, ecx
  3008.  
  3009.         dec     ecx
  3010.         jns     .start_x
  3011.  
  3012.         pop     ecx
  3013.         dec     ebx
  3014.         dec     edx
  3015.         jns     .start_y
  3016. ;--------------------------------------
  3017. align 4
  3018. .not_found:
  3019.         pop     ebp edi
  3020.         jmp     ricino
  3021. ;--------------------------------------
  3022. align 4
  3023. .found:
  3024.         pop     ebp edi
  3025.  
  3026.         mov     [edi + WDATA.fl_redraw], WSTATE_REDRAW  ; mark as redraw
  3027. ;--------------------------------------
  3028. align 4
  3029. ricino:
  3030. not_this_task:
  3031.         pop     ecx
  3032.  
  3033.         cmp     ecx, [thread_count]
  3034.         jle     newdw2
  3035.  
  3036.         pop     eax
  3037.         popad
  3038.         ret
  3039. ;-----------------------------------------------------------------------------
  3040. align 4
  3041. calculatebackground:   ; background
  3042.         mov     edi, [_display.win_map]              ; set os to use all pixels
  3043.         mov     eax, 0x01010101
  3044.         mov     ecx, [_display.win_map_size]
  3045.         shr     ecx, 2
  3046.         rep stosd
  3047.         mov     byte[background_window + WDATA.z_modif], ZPOS_DESKTOP
  3048.         mov     [REDRAW_BACKGROUND], 0
  3049.         ret
  3050. ;-----------------------------------------------------------------------------
  3051. uglobal
  3052.   imax    dd 0x0
  3053. endg
  3054. ;-----------------------------------------------------------------------------
  3055. align 4
  3056. delay_ms:     ; delay in 1/1000 sec
  3057.         pushad
  3058.  
  3059.         cmp     [hpet_base], 0
  3060.         jz      .no_hpet
  3061.         mov     eax, esi
  3062.         mov     edx, 1_000_000 ; ms to ns
  3063.         mul     edx
  3064.         mov     ebx, edx
  3065.         mov     ecx, eax
  3066.  
  3067.         push    ecx
  3068.         call    get_clock_ns
  3069.         pop     ecx
  3070.         mov     edi, edx
  3071.         mov     esi, eax
  3072. .wait:
  3073.         push    ecx
  3074.         call    get_clock_ns
  3075.         pop     ecx
  3076.         sub     eax, esi
  3077.         sbb     edx, edi
  3078.         sub     eax, ecx
  3079.         sbb     edx, ebx
  3080.         jc      .wait
  3081.         jmp     .done
  3082.  
  3083. .no_hpet:
  3084.         mov     ecx, esi
  3085.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  3086.         imul    ecx, 33941
  3087.         shr     ecx, 9
  3088.         ; </CPU clock fix>
  3089.  
  3090.         in      al, 0x61
  3091.         and     al, 0x10
  3092.         mov     ah, al
  3093.         cld
  3094.  
  3095. .cnt1:
  3096.         in      al, 0x61
  3097.         and     al, 0x10
  3098.         cmp     al, ah
  3099.         jz      .cnt1
  3100.  
  3101.         mov     ah, al
  3102.         loop    .cnt1
  3103.  
  3104. .done:
  3105.         popad
  3106.         ret
  3107. ;-----------------------------------------------------------------------------
  3108. align 4
  3109. set_app_param:
  3110.         mov     edi, [current_slot]
  3111.         xchg    ebx, [edi + APPDATA.event_mask] ; set new event mask
  3112.         mov     [esp + SYSCALL_STACK.eax], ebx  ; return old mask value
  3113.         ret
  3114. ;-----------------------------------------------------------------------------
  3115.  
  3116. ; this is for syscall
  3117. proc delay_hs_unprotected
  3118.         call    unprotect_from_terminate
  3119.         call    delay_hs
  3120.         call    protect_from_terminate
  3121.         ret
  3122. endp
  3123.  
  3124. if 1
  3125. align 4
  3126. delay_hs:     ; delay in 1/100 secs
  3127. ; ebx = delay time
  3128.         pushad
  3129.         push    ebx
  3130.         xor     esi, esi
  3131.         mov     ecx, MANUAL_DESTROY
  3132.         call    create_event
  3133.         test    eax, eax
  3134.         jz      .done
  3135.  
  3136.         mov     ebx, edx
  3137.         mov     ecx, [esp]
  3138.         push    edx
  3139.         push    eax
  3140.         call    wait_event_timeout
  3141.         pop     eax
  3142.         pop     ebx
  3143.         call    destroy_event
  3144. .done:
  3145.         add     esp, 4
  3146.         popad
  3147.         ret
  3148.  
  3149. else
  3150.  
  3151. align 4
  3152. delay_hs:     ; delay in 1/100 secs
  3153. ; ebx = delay time
  3154.         push    ecx
  3155.         push    edx
  3156.  
  3157.         mov     edx, [timer_ticks]
  3158. ;--------------------------------------
  3159. align 4
  3160. .newtic:
  3161.         mov     ecx, [timer_ticks]
  3162.         sub     ecx, edx
  3163.         cmp     ecx, ebx
  3164.         jae     .zerodelay
  3165.  
  3166.         call    change_task
  3167.  
  3168.         jmp     .newtic
  3169. ;--------------------------------------
  3170. align 4
  3171. .zerodelay:
  3172.         pop     edx
  3173.         pop     ecx
  3174.         ret
  3175. end if
  3176.  
  3177. ;-----------------------------------------------------------------------------
  3178. align 16        ;very often call this subrutine
  3179. memmove:       ; memory move in bytes
  3180. ; eax = from
  3181. ; ebx = to
  3182. ; ecx = no of bytes
  3183.         test    ecx, ecx
  3184.         jle     .ret
  3185.  
  3186.         push    esi edi ecx
  3187.  
  3188.         mov     edi, ebx
  3189.         mov     esi, eax
  3190.  
  3191.         test    ecx, not 11b
  3192.         jz      @f
  3193.  
  3194.         push    ecx
  3195.         shr     ecx, 2
  3196.         rep movsd
  3197.         pop     ecx
  3198.         and     ecx, 11b
  3199.         jz      .finish
  3200. ;--------------------------------------
  3201. align 4
  3202. @@:
  3203.         rep movsb
  3204. ;--------------------------------------
  3205. align 4
  3206. .finish:
  3207.         pop     ecx edi esi
  3208. ;--------------------------------------
  3209. align 4
  3210. .ret:
  3211.         ret
  3212. ;-----------------------------------------------------------------------------
  3213.  
  3214. ; in: eax = port
  3215. ;     ebp = subfunction
  3216. ;          0 - set access
  3217. ;          1 - clear access
  3218. ; out: not return value
  3219. align 4
  3220. set_io_access_rights:
  3221.         push    edi eax
  3222.         mov     edi, tss._io_map_0
  3223.  
  3224.         test    ebp, ebp         ; enable access - ebp = 0
  3225.         jnz     .siar1
  3226.  
  3227.         btr     [edi], eax
  3228.         pop     eax edi
  3229.         ret
  3230. .siar1:
  3231.         bts     [edi], eax      ; disable access - ebp = 1
  3232.         pop     eax edi
  3233.         ret
  3234.  
  3235. align 4
  3236. ; @brief ReservePortArea and FreePortArea
  3237. ; @param edx number end arrea of ports (include last number of port)
  3238. ; @param ecx number start arrea of ports
  3239. ; @param ebx sub function 0 - reserve, 1 - free
  3240. ; @param eax 46 - number function
  3241. ; @returns  eax = 0 - succesful eax = 1 - error
  3242. syscall_reserveportarea:        ; ReservePortArea and FreePortArea
  3243.  
  3244.         call    r_f_port_area
  3245.         mov     [esp + SYSCALL_STACK.eax], eax
  3246.         ret
  3247.  
  3248. ;reserve/free group of ports
  3249. ;  * eax = 46 - number function
  3250. ;  * ebx = 0 - reserve, 1 - free
  3251. ;  * ecx = number start arrea of ports
  3252. ;  * edx = number end arrea of ports (include last number of port)
  3253. ;Return value:
  3254. ;  * eax = 0 - succesful
  3255. ;  * eax = 1 - error
  3256. ;  * The system has reserve this ports:
  3257. ;    0..0x2d, 0x30..0x4d, 0x50..0xdf, 0xe5..0xff (include last number of port).
  3258. ;destroys eax,ebx, ebp
  3259. r_f_port_area:
  3260.  
  3261.         test    ebx, ebx
  3262.         jnz     .free_port_area
  3263.  
  3264.         cmp     ecx, edx      ; beginning > end ?
  3265.         ja      .err
  3266.         cmp     edx, 65536    ;test ebx, not 0xffff
  3267.         jae     .err
  3268.         mov     eax, [RESERVED_PORTS]
  3269.         test    eax, eax      ; no reserved areas ?
  3270.         je      .rpal2
  3271.         cmp     eax, 255      ; max reserved
  3272.         jae     .err
  3273.  .rpal3:
  3274.         mov     ebx, eax
  3275.         shl     ebx, 4   ;16 byte is sizeof item in RESERVED_PORTS table
  3276.         add     ebx, RESERVED_PORTS
  3277.         cmp     ecx, [ebx+8]
  3278.         ja      .rpal4
  3279.         cmp     edx, [ebx+4]
  3280.         jae     .err
  3281.  .rpal4:
  3282.         dec     eax
  3283.         jnz     .rpal3
  3284.         jmp     .rpal2
  3285. .err:
  3286.         xor     eax, eax
  3287.         inc     eax
  3288.         ret
  3289.  
  3290.    .rpal2:
  3291.      ; enable port access at port IO map
  3292.         pushad                        ; start enable io map
  3293.         mov     eax, ecx
  3294.         xor     ebp, ebp               ; enable - eax = port
  3295.         cli
  3296. .new_port_access:
  3297.         call    set_io_access_rights
  3298.  
  3299.         inc     eax
  3300.         cmp     eax, edx
  3301.         jbe     .new_port_access
  3302.  
  3303.         sti
  3304.         popad                         ; end enable io map
  3305.  
  3306.         mov     eax, [RESERVED_PORTS]
  3307.         inc     eax
  3308.         mov     [RESERVED_PORTS], eax
  3309.         shl     eax, 4
  3310.         add     eax, RESERVED_PORTS
  3311.         mov     ebx, [current_slot]
  3312.         mov     ebx, [ebx + APPDATA.tid]
  3313.         mov     [eax], ebx   ; tid
  3314.         mov     [eax+4], ecx ;start port
  3315.         mov     [eax+8], edx ;finish port
  3316.  
  3317.         xor     eax, eax
  3318.         ret
  3319.  
  3320. .free_port_area:
  3321.  
  3322.         mov     eax, [RESERVED_PORTS]; no reserved areas ?
  3323.         test    eax, eax
  3324.         jz      .frpal2
  3325.         mov     ebx, [current_slot]
  3326.         mov     ebx, [ebx + APPDATA.tid]
  3327.    .frpal3:
  3328.         mov     edi, eax
  3329.         shl     edi, 4
  3330.         add     edi, RESERVED_PORTS
  3331.         cmp     ebx, [edi]
  3332.         jne     .frpal4
  3333.         cmp     ecx, [edi+4]
  3334.         jne     .frpal4
  3335.         cmp     edx, [edi+8]
  3336.         jne     .frpal4
  3337.         jmp     .frpal1
  3338.    .frpal4:
  3339.         dec     eax
  3340.         jnz     .frpal3
  3341.    .frpal2:
  3342.         inc     eax
  3343.         ret
  3344.    .frpal1:
  3345.         push    ecx
  3346.         mov     ecx, 256
  3347.         sub     ecx, eax
  3348.         shl     ecx, 4
  3349.         mov     esi, edi
  3350.         add     esi, 16
  3351.         cld
  3352.         rep movsb
  3353.  
  3354.         dec     dword [RESERVED_PORTS]
  3355. ;disable port access at port IO map
  3356.                         ; start disable io map
  3357.         pop     eax     ;start port
  3358.         ;cmp     edx, 65536
  3359.         ;jge     no_mask_io
  3360.  
  3361.         xor     ebp, ebp
  3362.         inc     ebp
  3363. .new_port_access_disable:           ; disable - eax = port
  3364.         call    set_io_access_rights
  3365.  
  3366.         inc     eax
  3367.         cmp     eax, edx
  3368.         jbe     .new_port_access_disable
  3369.                                     ; end disable io map
  3370.         xor     eax, eax
  3371.         ret
  3372. ;-----------------------------------------------------------------------------
  3373. align 4
  3374. drawbackground:
  3375.         cmp     [BgrDrawMode], 1
  3376.         jne     .bgrstr
  3377.         call    vesa20_drawbackground_tiled
  3378.         call    __sys_draw_pointer
  3379.         ret
  3380. ;--------------------------------------
  3381. align 4
  3382. .bgrstr:
  3383.         call    vesa20_drawbackground_stretch
  3384.         call    __sys_draw_pointer
  3385.         ret
  3386. ;-----------------------------------------------------------------------------
  3387. align 4
  3388. syscall_putimage:                       ; PutImage
  3389. ; add check pointer
  3390.         push    ecx
  3391.         mov     ax, cx
  3392.         shr     ecx, 16
  3393.         imul    eax, ecx
  3394.         lea     eax, [eax*3]
  3395.         stdcall is_region_userspace, ebx, eax
  3396.         pop     ecx
  3397.         jnz     sys_putimage.exit
  3398.  
  3399. sys_putimage:
  3400.         test    ecx, 0x80008000
  3401.         jnz     .exit
  3402.         test    ecx, 0x0000FFFF
  3403.         jz      .exit
  3404.         test    ecx, 0xFFFF0000
  3405.         jnz     @f
  3406. ;--------------------------------------
  3407. align 4
  3408. .exit:
  3409.         ret
  3410. ;--------------------------------------
  3411. align 4
  3412. @@:
  3413.         mov     edi, [current_slot]
  3414.         mov     edi, [edi + APPDATA.window]
  3415.         add     dx, word[edi + WDATA.clientbox.top]
  3416.         rol     edx, 16
  3417.         add     dx, word[edi + WDATA.clientbox.left]
  3418.         rol     edx, 16
  3419. ;--------------------------------------
  3420. align 4
  3421. .forced:
  3422.         push    ebp esi 0
  3423.         mov     ebp, putimage_get24bpp
  3424.         mov     esi, putimage_init24bpp
  3425. ;--------------------------------------
  3426. align 4
  3427. sys_putimage_bpp:
  3428.         call    vesa20_putimage
  3429.         pop     ebp esi ebp
  3430.         ret
  3431. ;        jmp     [draw_pointer]
  3432. ;-----------------------------------------------------------------------------
  3433. align 4
  3434. sys_putimage_palette:
  3435. ; ebx = pointer to image
  3436. ; ecx = [xsize]*65536 + [ysize]
  3437. ; edx = [xstart]*65536 + [ystart]
  3438. ; esi = number of bits per pixel, must be 8, 24 or 32
  3439. ; edi = pointer to palette
  3440. ; ebp = row delta
  3441. ; check pointer
  3442.         push    ecx esi
  3443.         mov     ax, cx
  3444.         shr     ecx, 16
  3445.         imul    eax, ecx
  3446. ;        imul    eax, esi ; eax*count bit in 1 pixel
  3447. ;        shr     eax, 3
  3448.         stdcall is_region_userspace, ebx, eax
  3449.         pop     esi ecx
  3450.         jnz     sys_putimage.exit
  3451.  
  3452.         mov     eax, [current_slot]
  3453.         mov     eax, [eax + APPDATA.window]
  3454.         add     dx, word [eax + WDATA.clientbox.top]
  3455.         rol     edx, 16
  3456.         add     dx, word [eax + WDATA.clientbox.left]
  3457.         rol     edx, 16
  3458. ;--------------------------------------
  3459. align 4
  3460. .forced:
  3461.         cmp     esi, 1
  3462.         jnz     @f
  3463.         push    edi
  3464.         mov     eax, [edi+4]
  3465.         sub     eax, [edi]
  3466.         push    eax
  3467.         push    dword [edi]
  3468.         push    0ffffff80h
  3469.         mov     edi, esp
  3470.         call    put_mono_image
  3471.         add     esp, 12
  3472.         pop     edi
  3473.         ret
  3474. ;--------------------------------------
  3475. align 4
  3476. @@:
  3477.         cmp     esi, 2
  3478.         jnz     @f
  3479.         push    edi
  3480.         push    0ffffff80h
  3481.         mov     edi, esp
  3482.         call    put_2bit_image
  3483.         pop     eax
  3484.         pop     edi
  3485.         ret
  3486. ;--------------------------------------
  3487. align 4
  3488. @@:
  3489.         cmp     esi, 4
  3490.         jnz     @f
  3491.         push    edi
  3492.         push    0ffffff80h
  3493.         mov     edi, esp
  3494.         call    put_4bit_image
  3495.         pop     eax
  3496.         pop     edi
  3497.         ret
  3498. ;--------------------------------------
  3499. align 4
  3500. @@:
  3501.         push    ebp esi ebp
  3502.         cmp     esi, 8
  3503.         jnz     @f
  3504.         mov     ebp, putimage_get8bpp
  3505.         mov     esi, putimage_init8bpp
  3506.         jmp     sys_putimage_bpp
  3507. ;--------------------------------------
  3508. align 4
  3509. @@:
  3510.         cmp     esi, 9
  3511.         jnz     @f
  3512.         mov     ebp, putimage_get9bpp
  3513.         mov     esi, putimage_init9bpp
  3514.         jmp     sys_putimage_bpp
  3515. ;--------------------------------------
  3516. align 4
  3517. @@:
  3518.         cmp     esi, 15
  3519.         jnz     @f
  3520.         mov     ebp, putimage_get15bpp
  3521.         mov     esi, putimage_init15bpp
  3522.         jmp     sys_putimage_bpp
  3523. ;--------------------------------------
  3524. align 4
  3525. @@:
  3526.         cmp     esi, 16
  3527.         jnz     @f
  3528.         mov     ebp, putimage_get16bpp
  3529.         mov     esi, putimage_init16bpp
  3530.         jmp     sys_putimage_bpp
  3531. ;--------------------------------------
  3532. align 4
  3533. @@:
  3534.         cmp     esi, 24
  3535.         jnz     @f
  3536.         mov     ebp, putimage_get24bpp
  3537.         mov     esi, putimage_init24bpp
  3538.         jmp     sys_putimage_bpp
  3539. ;--------------------------------------
  3540. align 4
  3541. @@:
  3542.         cmp     esi, 32
  3543.         jnz     @f
  3544.         mov     ebp, putimage_get32bpp
  3545.         mov     esi, putimage_init32bpp
  3546.         jmp     sys_putimage_bpp
  3547. ;--------------------------------------
  3548. align 4
  3549. @@:
  3550.         pop     ebp esi ebp
  3551.         ret
  3552. ;-----------------------------------------------------------------------------
  3553. align 4
  3554. put_mono_image:
  3555.         push    ebp esi ebp
  3556.         mov     ebp, putimage_get1bpp
  3557.         mov     esi, putimage_init1bpp
  3558.         jmp     sys_putimage_bpp
  3559. ;-----------------------------------------------------------------------------
  3560. align 4
  3561. put_2bit_image:
  3562.         push    ebp esi ebp
  3563.         mov     ebp, putimage_get2bpp
  3564.         mov     esi, putimage_init2bpp
  3565.         jmp     sys_putimage_bpp
  3566. ;-----------------------------------------------------------------------------
  3567. align 4
  3568. put_4bit_image:
  3569.         push    ebp esi ebp
  3570.         mov     ebp, putimage_get4bpp
  3571.         mov     esi, putimage_init4bpp
  3572.         jmp     sys_putimage_bpp
  3573. ;-----------------------------------------------------------------------------
  3574. align 4
  3575. putimage_init24bpp:
  3576.         lea     eax, [eax*3]
  3577. putimage_init8bpp:
  3578. putimage_init9bpp:
  3579.         ret
  3580. ;-----------------------------------------------------------------------------
  3581. align 16
  3582. putimage_get24bpp:
  3583.         movzx   eax, byte [esi+2]
  3584.         shl     eax, 16
  3585.         mov     ax, [esi]
  3586.         add     esi, 3
  3587.         ret     4
  3588. ;-----------------------------------------------------------------------------
  3589. align 16
  3590. putimage_get8bpp:
  3591.         movzx   eax, byte [esi]
  3592.         push    edx
  3593.         mov     edx, [esp+8]
  3594.         mov     eax, [edx + eax*4]
  3595.         pop     edx
  3596.         inc     esi
  3597.         ret     4
  3598. ;-----------------------------------------------------------------------------
  3599. align 16
  3600. putimage_get9bpp:
  3601.         lodsb
  3602.         mov     ah, al
  3603.         shl     eax, 8
  3604.         mov     al, ah
  3605.         ret     4
  3606. ;-----------------------------------------------------------------------------
  3607. align 4
  3608. putimage_init1bpp:
  3609.         add     eax, ecx
  3610.         push    ecx
  3611.         add     eax, 7
  3612.         add     ecx, 7
  3613.         shr     eax, 3
  3614.         shr     ecx, 3
  3615.         sub     eax, ecx
  3616.         pop     ecx
  3617.         ret
  3618. ;-----------------------------------------------------------------------------
  3619. align 16
  3620. putimage_get1bpp:
  3621.         push    edx
  3622.         mov     edx, [esp+8]
  3623.         mov     al, [edx]
  3624.         add     al, al
  3625.         jnz     @f
  3626.         lodsb
  3627.         adc     al, al
  3628. @@:
  3629.         mov     [edx], al
  3630.         sbb     eax, eax
  3631.         and     eax, [edx+8]
  3632.         add     eax, [edx+4]
  3633.         pop     edx
  3634.         ret     4
  3635. ;-----------------------------------------------------------------------------
  3636. align 4
  3637. putimage_init2bpp:
  3638.         add     eax, ecx
  3639.         push    ecx
  3640.         add     ecx, 3
  3641.         add     eax, 3
  3642.         shr     ecx, 2
  3643.         shr     eax, 2
  3644.         sub     eax, ecx
  3645.         pop     ecx
  3646.         ret
  3647. ;-----------------------------------------------------------------------------
  3648. align 16
  3649. putimage_get2bpp:
  3650.         push    edx
  3651.         mov     edx, [esp+8]
  3652.         mov     al, [edx]
  3653.         mov     ah, al
  3654.         shr     al, 6
  3655.         shl     ah, 2
  3656.         jnz     .nonewbyte
  3657.         lodsb
  3658.         mov     ah, al
  3659.         shr     al, 6
  3660.         shl     ah, 2
  3661.         add     ah, 1
  3662. .nonewbyte:
  3663.         mov     [edx], ah
  3664.         mov     edx, [edx+4]
  3665.         movzx   eax, al
  3666.         mov     eax, [edx + eax*4]
  3667.         pop     edx
  3668.         ret     4
  3669. ;-----------------------------------------------------------------------------
  3670. align 4
  3671. putimage_init4bpp:
  3672.         add     eax, ecx
  3673.         push    ecx
  3674.         add     ecx, 1
  3675.         inc     eax      ;add   eax, 1
  3676.         shr     ecx, 1
  3677.         shr     eax, 1
  3678.         sub     eax, ecx
  3679.         pop     ecx
  3680.         ret
  3681. ;-----------------------------------------------------------------------------
  3682. align 16
  3683. putimage_get4bpp:
  3684.         push    edx
  3685.         mov     edx, [esp+8]
  3686.         add     byte [edx], 80h
  3687.         jc      @f
  3688.         movzx   eax, byte [edx+1]
  3689.         mov     edx, [edx+4]
  3690.         and     eax, 0x0F
  3691.         mov     eax, [edx + eax*4]
  3692.         pop     edx
  3693.         ret     4
  3694. @@:
  3695.         movzx   eax, byte [esi]
  3696.         add     esi, 1
  3697.         mov     [edx+1], al
  3698.         shr     eax, 4
  3699.         mov     edx, [edx+4]
  3700.         mov     eax, [edx + eax*4]
  3701.         pop     edx
  3702.         ret     4
  3703. ;-----------------------------------------------------------------------------
  3704. align 4
  3705. putimage_init32bpp:
  3706.         shl     eax, 2
  3707.         ret
  3708. ;-----------------------------------------------------------------------------
  3709. align 16
  3710. putimage_get32bpp:
  3711.         lodsd
  3712.         ret     4
  3713. ;-----------------------------------------------------------------------------
  3714. align 4
  3715. putimage_init15bpp:
  3716. putimage_init16bpp:
  3717.         add     eax, eax
  3718.         ret
  3719. ;-----------------------------------------------------------------------------
  3720. align 16
  3721. putimage_get15bpp:
  3722. ; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
  3723.         push    ecx edx
  3724.         movzx   eax, word [esi]
  3725.         add     esi, 2
  3726.         mov     ecx, eax
  3727.         mov     edx, eax
  3728.         and     eax, 0x1F
  3729.         and     ecx, 0x1F shl 5
  3730.         and     edx, 0x1F shl 10
  3731.         shl     eax, 3
  3732.         shl     ecx, 6
  3733.         shl     edx, 9
  3734.         or      eax, ecx
  3735.         or      eax, edx
  3736.         pop     edx ecx
  3737.         ret     4
  3738. ;-----------------------------------------------------------------------------
  3739. align 16
  3740. putimage_get16bpp:
  3741. ; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
  3742.         push    ecx edx
  3743.         movzx   eax, word [esi]
  3744.         add     esi, 2
  3745.         mov     ecx, eax
  3746.         mov     edx, eax
  3747.         and     eax, 0x1F
  3748.         and     ecx, 0x3F shl 5
  3749.         and     edx, 0x1F shl 11
  3750.         shl     eax, 3
  3751.         shl     ecx, 5
  3752.         shl     edx, 8
  3753.         or      eax, ecx
  3754.         or      eax, edx
  3755.         pop     edx ecx
  3756.         ret     4
  3757. ;-----------------------------------------------------------------------------
  3758. ;align 4
  3759. ; eax x beginning
  3760. ; ebx y beginning
  3761. ; ecx x end
  3762.         ; edx y end
  3763. ; edi color
  3764. ;__sys_drawbar:
  3765. ;        mov     esi, [current_slot]
  3766. ;        mov     esi, [esi + APPDATA.window]
  3767. ;        add     eax, [esi + WDATA.clientbox.left]
  3768. ;        add     ecx, [esi + WDATA.clientbox.left]
  3769. ;        add     ebx, [esi + WDATA.clientbox.top]
  3770. ;        add     edx, [esi + WDATA.clientbox.top]
  3771. ;--------------------------------------
  3772. ;align 4
  3773. ;.forced:
  3774. ;        call    vesa20_drawbar
  3775. ;        call    [draw_pointer]
  3776. ;        ret
  3777. ;-----------------------------------------------------------------------------
  3778. if used _rdtsc
  3779. _rdtsc:
  3780.         bt      [cpu_caps], CAPS_TSC
  3781.         jnc     .ret_rdtsc
  3782.         rdtsc
  3783.         ret
  3784.    .ret_rdtsc:
  3785.         mov     edx, 0xffffffff
  3786.         mov     eax, 0xffffffff
  3787.         ret
  3788. end if
  3789.  
  3790. sys_msg_board_str:
  3791.  
  3792.         pushad
  3793.    @@:
  3794.         cmp     [esi], byte 0
  3795.         je      @f
  3796.         mov     ebx, 1
  3797.         movzx   ecx, byte [esi]
  3798.         call    sys_msg_board
  3799.         inc     esi
  3800.         jmp     @b
  3801.    @@:
  3802.         popad
  3803.         ret
  3804.  
  3805. sys_msg_board_byte:
  3806. ; in: al = byte to display
  3807. ; out: nothing
  3808. ; destroys: nothing
  3809.         pushad
  3810.         mov     ecx, 2
  3811.         shl     eax, 24
  3812.         jmp     @f
  3813.  
  3814. sys_msg_board_word:
  3815. ; in: ax = word to display
  3816. ; out: nothing
  3817. ; destroys: nothing
  3818.         pushad
  3819.         mov     ecx, 4
  3820.         shl     eax, 16
  3821.         jmp     @f
  3822.  
  3823. sys_msg_board_dword:
  3824. ; in: eax = dword to display
  3825. ; out: nothing
  3826. ; destroys: nothing
  3827.         pushad
  3828.         mov     ecx, 8
  3829. @@:
  3830.         push    ecx
  3831.         rol     eax, 4
  3832.         push    eax
  3833.         and     al, 0xF
  3834.         cmp     al, 10
  3835.         sbb     al, 69h
  3836.         das
  3837.         mov     cl, al
  3838.         xor     ebx, ebx
  3839.         inc     ebx
  3840.         call    sys_msg_board
  3841.         pop     eax
  3842.         pop     ecx
  3843.         loop    @b
  3844.         popad
  3845.         ret
  3846.  
  3847. msg_board_data_size = 65536 ; Must be power of two
  3848.  
  3849. uglobal
  3850. msg_board_data  rb  msg_board_data_size
  3851. msg_board_count dd  ?
  3852. endg
  3853.  
  3854. iglobal
  3855. msg_board_pos   dd  42*6*65536+10 ; for printing debug output on the screen
  3856. endg
  3857.  
  3858. sys_msg_board:
  3859. ; ebx=1 -> write, cl = byte to write
  3860. ; ebx=2 -> read, ecx=0 -> no data, ecx=1 -> data in al
  3861.         push    eax ebx
  3862.         mov     eax, ebx
  3863.         mov     ebx, ecx
  3864.         mov     ecx, [msg_board_count]
  3865.         cmp     eax, 1
  3866.         jne     .read
  3867.  
  3868. if defined debug_com_base
  3869.         push    dx ax
  3870. @@: ; wait for empty transmit register
  3871.         mov     dx, debug_com_base+5
  3872.         in      al, dx
  3873.         test    al, 1 shl 5
  3874.         jz      @r
  3875.         mov     dx, debug_com_base      ; Output the byte
  3876.         mov     al, bl
  3877.         out     dx, al
  3878.         pop     ax dx
  3879. end if
  3880.  
  3881.         mov     [msg_board_data+ecx], bl
  3882.         cmp     byte [debug_direct_print], 1
  3883.         jnz     .end
  3884.         pusha
  3885.         lea     edx, [msg_board_data+ecx]
  3886.         mov     ecx, 0x40FFFFFF
  3887.         mov     ebx, [msg_board_pos]
  3888.         mov     edi, 1
  3889.         mov     esi, edi ;1
  3890.         call    dtext
  3891.         popa
  3892.         add     word [msg_board_pos+2], 6
  3893.         cmp     word [msg_board_pos+2], 105*6
  3894.         jnc     @f
  3895.         cmp     bl, 10
  3896.         jnz     .end
  3897. @@:
  3898.         mov     word [msg_board_pos+2], 42*6
  3899.         add     word [msg_board_pos], 10
  3900.         mov     eax, [_display.height]
  3901.         sub     eax, 10
  3902.         cmp     ax, word [msg_board_pos]
  3903.         jnc     @f
  3904.         mov     word [msg_board_pos], 10
  3905. @@:
  3906.         pusha
  3907.         mov     eax, [msg_board_pos]
  3908.         movzx   ebx, ax
  3909.         shr     eax, 16
  3910.         mov     edx, 105*6
  3911.         xor     ecx, ecx
  3912.         mov     edi, 1
  3913.         mov     esi, 9
  3914. @@:
  3915.         call    hline
  3916.         inc     ebx
  3917.         dec     esi
  3918.         jnz     @b
  3919.         popa
  3920. .end:
  3921.         inc     ecx
  3922.         and     ecx, msg_board_data_size - 1
  3923.         mov     [msg_board_count], ecx
  3924. .ret:
  3925.         pop     ebx eax
  3926.         ret
  3927.  
  3928. .read:
  3929.         cmp     eax, 2
  3930.         jne     .ret
  3931.         add     esp, 8  ; returning data in ebx and eax, so no need to restore them
  3932.         test    ecx, ecx
  3933.         jnz     @f
  3934.         mov     [esp + SYSCALL_STACK.eax], ecx
  3935.         mov     [esp + SYSCALL_STACK.ebx], ecx
  3936.         ret
  3937. @@:
  3938.         mov     eax, msg_board_data+1
  3939.         mov     ebx, msg_board_data
  3940.         movzx   edx, byte [ebx]
  3941.         call    memmove
  3942.         dec     [msg_board_count]
  3943.         mov     [esp + SYSCALL_STACK.eax], edx
  3944.         mov     [esp + SYSCALL_STACK.ebx], 1
  3945.         ret
  3946.  
  3947. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3948. ;; 61 sys function.                                                ;;
  3949. ;; in eax=61,ebx in [1..3]                                         ;;
  3950. ;; out eax                                                         ;;
  3951. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3952. iglobal
  3953. align 4
  3954. f61call:
  3955.            dd sys_gs.1   ; resolution
  3956.            dd sys_gs.2   ; bits per pixel
  3957.            dd sys_gs.3   ; bytes per scanline
  3958. endg
  3959.  
  3960.  
  3961. align 4
  3962.  
  3963. sys_gs:                         ; direct screen access
  3964.         dec     ebx
  3965.         cmp     ebx, 2
  3966.         ja      .not_support
  3967.         jmp     dword [f61call + ebx*4]
  3968. .not_support:
  3969.         or      [esp + SYSCALL_STACK.eax], -1
  3970.         ret
  3971.  
  3972.  
  3973. .1:                             ; resolution
  3974.         mov     eax, [_display.width]
  3975.         shl     eax, 16
  3976.         mov     ax, word [_display.height]
  3977.         mov     [esp + SYSCALL_STACK.eax], eax
  3978.         ret
  3979. .2:                             ; bits per pixel
  3980.         mov     eax, [_display.bits_per_pixel]
  3981.         mov     [esp + SYSCALL_STACK.eax], eax
  3982.         ret
  3983. .3:                             ; bytes per scanline
  3984.         mov     eax, [_display.lfb_pitch]
  3985.         mov     [esp + SYSCALL_STACK.eax], eax
  3986.         ret
  3987.  
  3988. align 4
  3989. syscall_getscreensize:                  ; GetScreenSize
  3990.         mov     ax, word [_display.width]
  3991.         dec     ax
  3992.         shl     eax, 16
  3993.         mov     ax, word [_display.height]
  3994.         dec     ax
  3995.         mov     [esp + SYSCALL_STACK.eax], eax
  3996.         ret
  3997. ;-----------------------------------------------------------------------------
  3998. align 4
  3999. syscall_cdaudio:
  4000. ; ECX - position of CD/DVD-drive
  4001. ; from 0=Primary Master to 3=Secondary Slave for first IDE contr.
  4002. ; from 4=Primary Master to 7=Secondary Slave for second IDE contr.
  4003. ; from 8=Primary Master to 11=Secondary Slave for third IDE contr.
  4004.         cmp     ecx, 11
  4005.         ja      .exit
  4006.  
  4007.         mov     eax, ecx
  4008.         shr     eax, 2
  4009.         lea     eax, [eax*5]
  4010.         mov     al, [eax + DRIVE_DATA + 1]
  4011.  
  4012.         push    ecx ebx
  4013.         mov     ebx, ecx
  4014.         and     ebx, 11b
  4015.         shl     ebx, 1
  4016.         mov     cl, 6
  4017.         sub     cl, bl
  4018.         shr     al, cl
  4019.         test    al, 2 ; it's not an ATAPI device
  4020.         pop     ebx ecx
  4021.  
  4022.         jz      .exit
  4023.  
  4024.         cmp     ebx, 4
  4025.         je      .eject
  4026.  
  4027.         cmp     ebx, 5
  4028.         je      .load
  4029. ;--------------------------------------
  4030. .exit:
  4031.         ret
  4032. ;--------------------------------------
  4033. .load:
  4034.         call    .reserve
  4035.         call    LoadMedium
  4036.         jmp     .free
  4037. ;--------------------------------------
  4038. .eject:
  4039.         call    .reserve
  4040.         call    clear_CD_cache
  4041.         call    allow_medium_removal
  4042.         call    EjectMedium
  4043.         jmp     .free
  4044. ;--------------------------------------
  4045. .reserve:
  4046.         call    reserve_cd
  4047.  
  4048.         mov     ebx, ecx
  4049.         inc     ebx
  4050.         mov     [cdpos], ebx
  4051.  
  4052.         mov     eax, ecx
  4053.         shr     eax, 1
  4054.         and     eax, 1
  4055.         inc     eax
  4056.         mov     [ChannelNumber], al
  4057.         mov     eax, ecx
  4058.         and     eax, 1
  4059.         mov     [DiskNumber], al
  4060.         call    reserve_cd_channel
  4061.         ret
  4062. ;--------------------------------------
  4063. .free:
  4064.         call    free_cd_channel
  4065.         and     [cd_status], 0
  4066.         ret
  4067. ;-----------------------------------------------------------------------------
  4068. align 4
  4069. syscall_getpixel_WinMap:                       ; GetPixel WinMap
  4070.         xor     eax, eax
  4071.  
  4072.         cmp     ebx, [_display.width]
  4073.         jae     .store
  4074.         cmp     ecx, [_display.height]
  4075.         jae     .store
  4076. ;--------------------------------------
  4077.         mov     eax, [d_width_calc_area + ecx*4]
  4078.         add     eax, [_display.win_map]
  4079.         movzx   eax, byte[eax+ebx]        ; get value for current point
  4080. ;--------------------------------------
  4081. align 4
  4082. .store:
  4083.         mov     [esp + SYSCALL_STACK.eax], eax
  4084.         ret
  4085. ;-----------------------------------------------------------------------------
  4086. align 4
  4087. syscall_getpixel:                       ; GetPixel
  4088.         mov     ecx, [_display.width]
  4089.         xor     edx, edx
  4090.         mov     eax, ebx
  4091.         div     ecx
  4092.         mov     ebx, edx
  4093.         xchg    eax, ebx
  4094.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  4095.         call    dword [GETPIXEL]; eax - x, ebx - y
  4096.         mov     [esp + SYSCALL_STACK.eax], ecx
  4097.         ret
  4098. ;-----------------------------------------------------------------------------
  4099. align 4
  4100. syscall_getarea:
  4101. ;eax = 36
  4102. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  4103. ;ecx = [size x]*65536 + [size y]
  4104. ;edx = [start x]*65536 + [start y]
  4105.         pushad
  4106.         mov     edi, ebx
  4107.         mov     eax, edx
  4108.         shr     eax, 16
  4109.         mov     ebx, edx
  4110.         and     ebx, 0xffff
  4111.         dec     eax
  4112.         dec     ebx
  4113.      ; eax - x, ebx - y
  4114.         mov     edx, ecx
  4115.  
  4116.         shr     ecx, 16
  4117.         and     edx, 0xffff
  4118.         mov     esi, ecx
  4119.      ; ecx - size x, edx - size y
  4120.  
  4121.         mov     ebp, edx
  4122.         lea     ebp, [ebp*3]
  4123.         imul    ebp, esi
  4124.         stdcall is_region_userspace, edi, ebp
  4125.         jnz     .exit
  4126.  
  4127.         mov     ebp, edx
  4128.         dec     ebp
  4129.         lea     ebp, [ebp*3]
  4130.  
  4131.         imul    ebp, esi
  4132.  
  4133.         mov     esi, ecx
  4134.         dec     esi
  4135.         lea     esi, [esi*3]
  4136.  
  4137.         add     ebp, esi
  4138.         add     ebp, edi
  4139.  
  4140.         add     ebx, edx
  4141. ;--------------------------------------
  4142. align 4
  4143. .start_y:
  4144.         push    ecx edx
  4145. ;--------------------------------------
  4146. align 4
  4147. .start_x:
  4148.         push    eax ebx ecx
  4149.         add     eax, ecx
  4150.  
  4151.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  4152.         call    dword [GETPIXEL]; eax - x, ebx - y
  4153.  
  4154.         mov     [ebp], cx
  4155.         shr     ecx, 16
  4156.         mov     [ebp+2], cl
  4157.  
  4158.         pop     ecx ebx eax
  4159.         sub     ebp, 3
  4160.         dec     ecx
  4161.         jnz     .start_x
  4162.         pop     edx ecx
  4163.         dec     ebx
  4164.         dec     edx
  4165.         jnz     .start_y
  4166.  
  4167. .exit:
  4168.         popad
  4169.         ret
  4170. ;-----------------------------------------------------------------------------
  4171.  
  4172. align 4
  4173. syscall_threads:
  4174. ; eax = 51
  4175. ; if ebx = 1  CreateThreads
  4176. ; ecx = thread entry point
  4177. ; edx = thread stack pointer
  4178. ; return eax = pid
  4179.  
  4180. ; if ebx = 2 GetCurrentThreadId
  4181. ; return eax = current id slot
  4182.  
  4183. ; if ebx = 3 GetThreadPriority
  4184. ; ecx = -1 curr slot  or  id slot
  4185. ; return eax = priority
  4186.  
  4187. ; if ebx = 4 SetThreadPriority
  4188. ; ecx = -1 curr slot or id slot
  4189. ; edx = set priority
  4190. ; return eax = old priority
  4191.  
  4192.         dec     ebx
  4193.         jz      .create
  4194.         mov     eax, [current_slot_idx]
  4195.         dec     ebx
  4196.         jz      .end  ;get_curr_slot
  4197.  
  4198.         test    ecx, ecx  ;-1 curr slot
  4199.         jl      .cur_slot
  4200.         mov     eax, ecx
  4201. .cur_slot:
  4202.         shl     eax, BSF sizeof.APPDATA
  4203.         add     eax, SLOT_BASE
  4204.  
  4205.         test    ecx, ecx  ;-1 curr slot
  4206.         jl      .curr_slot
  4207.  
  4208.         mov     esi, .err_exit
  4209.         mov     edi, CONTROL_EXCEPTION
  4210.  
  4211.         movzx   ecx, [eax+APPDATA.state]
  4212.         test    ecx, ecx
  4213.         jnz     .check_state
  4214.  
  4215.         cmp     [eax+APPDATA.tid], ecx
  4216.         jnz     .curr_slot
  4217.         jmp     .err_exit
  4218.  
  4219. .check_state:
  4220.         sub     ecx, 3 ;TSTATE_ZOMBIE
  4221.         jl      .curr_slot
  4222.         je      .err_exit
  4223.         dec     ecx ; 4 TSTATE_TERMINATING
  4224.         dec     ecx ; 5 TSTATE_WAITING
  4225.         jnz     .err_exit
  4226.  
  4227. .curr_slot:
  4228.         dec     ebx
  4229.         jz      .get_priority
  4230.         dec     ebx
  4231.         jz      .set_priority
  4232.  
  4233. .err_exit:
  4234.         push    -1
  4235.         pop     eax
  4236.         jmp     .end
  4237.  
  4238. .set_priority:  ;;sysfn 51,4
  4239.         mov     dh, dl
  4240.         lock xchg word [eax+APPDATA.def_priority], dx
  4241.         movzx   eax, dl ;old priority
  4242.         jmp     .end
  4243.  
  4244. .get_priority:  ;;sysfn 51,3
  4245.         movzx   eax, [eax+APPDATA.def_priority]
  4246.         jmp     .end
  4247.  
  4248. .create:        ;sysfn 51,1
  4249.         call    new_sys_threads
  4250.  
  4251. .end:
  4252.         mov     [esp + SYSCALL_STACK.eax], eax
  4253.         ret
  4254.  
  4255. ;------------------------------------------------------------------------------
  4256. align 4
  4257. calculate_fast_getting_offset_for_WinMapAddress:
  4258. ; calculate data area for fast getting offset to _WinMapAddress
  4259.         xor     eax, eax
  4260.         mov     ecx, [_display.height]
  4261.         mov     edi, d_width_calc_area
  4262.         cld
  4263. @@:
  4264.         stosd
  4265.         add     eax, [_display.width]
  4266.         dec     ecx
  4267.         jnz     @r
  4268.         ret
  4269. ;------------------------------------------------------------------------------
  4270. align 4
  4271. calculate_fast_getting_offset_for_LFB:
  4272. ; calculate data area for fast getting offset to LFB
  4273.         xor     eax, eax
  4274.         mov     ecx, [_display.height]
  4275.         mov     edi, BPSLine_calc_area
  4276.         cld
  4277. @@:
  4278.         stosd
  4279.         add     eax, [_display.lfb_pitch]
  4280.         dec     ecx
  4281.         jnz     @r
  4282.         ret
  4283. ;------------------------------------------------------------------------------
  4284. align 4
  4285. set_screen:
  4286. ; in:
  4287. ; eax - new Screen_Max_X
  4288. ; ecx - new BytesPerScanLine
  4289. ; edx - new Screen_Max_Y
  4290.  
  4291.         pushfd
  4292.         cli
  4293.  
  4294.         mov     [_display.lfb_pitch], ecx
  4295.  
  4296.         mov     [screen_workarea.right], eax
  4297.         mov     [screen_workarea.bottom], edx
  4298.  
  4299.         push    ebx
  4300.         push    esi
  4301.         push    edi
  4302.  
  4303.         pushad
  4304.  
  4305.         cmp     [do_not_touch_winmap], 1
  4306.         je      @f
  4307.  
  4308.         stdcall kernel_free, [_display.win_map]
  4309.  
  4310.         mov     eax, [_display.width]
  4311.         mul     [_display.height]
  4312.         mov     [_display.win_map_size], eax
  4313.  
  4314.         stdcall kernel_alloc, eax
  4315.         mov     [_display.win_map], eax
  4316.         test    eax, eax
  4317.         jz      .epic_fail
  4318. ; store for f.18.24
  4319.         mov     eax, [_display.width]
  4320.         mov     [display_width_standard], eax
  4321.  
  4322.         mov     eax, [_display.height]
  4323.         mov     [display_height_standard], eax
  4324. @@:
  4325.         call    calculate_fast_getting_offset_for_WinMapAddress
  4326. ; for Qemu or non standart video cards
  4327. ; Unfortunately [BytesPerScanLine] does not always
  4328. ;                             equal to [_display.width] * [ScreenBPP] / 8
  4329.         call    calculate_fast_getting_offset_for_LFB
  4330.         popad
  4331.  
  4332.         call    repos_windows
  4333.         xor     eax, eax
  4334.         xor     ebx, ebx
  4335.         mov     ecx, [_display.width]
  4336.         mov     edx, [_display.height]
  4337.         dec     ecx
  4338.         dec     edx
  4339.         call    calculatescreen
  4340.         pop     edi
  4341.         pop     esi
  4342.         pop     ebx
  4343.  
  4344.         popfd
  4345.         ret
  4346.  
  4347. .epic_fail:
  4348.         hlt                     ; Houston, we've had a problem
  4349.  
  4350. ; --------------- APM ---------------------
  4351. uglobal
  4352. apm_entry       dp      0
  4353. apm_vf          dd      0
  4354. endg
  4355.  
  4356. align 4
  4357. sys_apm:
  4358.         xor     eax, eax
  4359.         cmp     word [apm_vf], ax       ; Check APM BIOS enable
  4360.         jne     @f
  4361.         inc     eax
  4362.         or      dword [esp + 44], eax   ; error
  4363.         add     eax, 7
  4364.         mov     [esp + SYSCALL_STACK.eax], eax  ; 32-bit protected-mode
  4365.                                                 ; interface not supported
  4366.         ret
  4367.  
  4368. @@:
  4369. ;       xchg    eax, ecx
  4370. ;       xchg    ebx, ecx
  4371.  
  4372.         cmp     dx, 3
  4373.         ja      @f
  4374.         and     [esp + 44], byte 0xfe    ; emulate func 0..3 as func 0
  4375.         mov     eax, [apm_vf]
  4376.         mov     [esp + SYSCALL_STACK.eax], eax
  4377.         shr     eax, 16
  4378.         mov     [esp + SYSCALL_STACK.ecx], eax
  4379.         ret
  4380.  
  4381. @@:
  4382.  
  4383.         mov     esi, [master_tab+(OS_BASE shr 20)]
  4384.         xchg    [master_tab], esi
  4385.         push    esi
  4386.         mov     edi, cr3
  4387.         mov     cr3, edi                ;flush TLB
  4388.  
  4389.         call    pword [apm_entry]       ;call APM BIOS
  4390.  
  4391.         xchg    eax, [esp]
  4392.         mov     [master_tab], eax
  4393.         mov     eax, cr3
  4394.         mov     cr3, eax
  4395.         pop     eax
  4396.  
  4397.         mov     [esp + SYSCALL_STACK.edi], edi
  4398.         mov     [esp + SYSCALL_STACK.esi], esi
  4399.         mov     [esp + SYSCALL_STACK.ebx], ebx
  4400.         mov     [esp + SYSCALL_STACK.edx], edx
  4401.         mov     [esp + SYSCALL_STACK.ecx], ecx
  4402.         mov     [esp + SYSCALL_STACK.eax], eax
  4403.         setc    al
  4404.         and     [esp + 44], byte 0xfe
  4405.         or      [esp + 44], al
  4406.         ret
  4407. ; -----------------------------------------
  4408.  
  4409. align 4
  4410. undefined_syscall:                      ; Undefined system call
  4411.         mov     [esp + SYSCALL_STACK.eax], -1
  4412.         ret
  4413.  
  4414. align 4
  4415. ; @brief Check if given memory region lays in lower 2gb (userspace memory) or not
  4416. ; @param base Base address of region
  4417. ; @param len Lenght of region
  4418. ; @return ZF = 1 if region in userspace memory,
  4419. ;         ZF = 0 otherwise
  4420. proc is_region_userspace stdcall, base:dword, len:dword
  4421.         push    eax
  4422.         mov     eax, [base]
  4423.  
  4424.         cmp     eax, OS_BASE-1
  4425.         ja      @f              ; zf
  4426.  
  4427.         add     eax, [len]
  4428.         jc      @f              ; zf
  4429.         cmp     eax, OS_BASE
  4430.         ja      @f              ; zf
  4431.  
  4432.         cmp     eax, eax        ; ZF
  4433. @@:
  4434.         pop     eax
  4435.         ret
  4436. endp
  4437.  
  4438. align 4
  4439. ; @brief Check whether given string lays in userspace memory, i.e. below OS_BASE
  4440. ; @param base Base address of string
  4441. ; @return ZF = 1 if string in userspace memory,
  4442. ;         zf = 0 otherwise
  4443. proc is_string_userspace stdcall, base:dword
  4444.         push    eax ecx edi
  4445.         xor     eax, eax
  4446.         mov     edi, [base]
  4447.  
  4448.         mov     ecx, OS_BASE-1
  4449.         sub     ecx, edi
  4450.         jb      .done           ; zf
  4451.         inc     ecx
  4452.         cmp     ecx, 0x10000    ; don't allow strings larger than 64k?
  4453.         jbe     @f
  4454.         mov     ecx, 0x10000
  4455. @@:
  4456.         repnz scasb
  4457. .done:
  4458.         pop     edi ecx eax
  4459.         ret
  4460. endp
  4461.  
  4462. if ~ lang eq sp
  4463. diff16 "end of .text segment",0,$
  4464. end if
  4465.  
  4466. include "data32.inc"
  4467.  
  4468. __REV__ = __REV
  4469.  
  4470. if ~ lang eq sp
  4471. diff16 "end of kernel code",0,$
  4472. end if
  4473.