Subversion Repositories Kolibri OS

Rev

Rev 9828 | Rev 9897 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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