Subversion Repositories Kolibri OS

Rev

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