Subversion Repositories Kolibri OS

Rev

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

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