Subversion Repositories Kolibri OS

Rev

Rev 9950 | Rev 9964 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2288 clevermous 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;
9958 dunkaist 3
;; Copyright (C) KolibriOS team 2004-2024. All rights reserved.
9947 ace_dent 4
;; Kernel programmers are acknowledged in CREDITS.TXT
2288 clevermous 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
 
3383 hidnplayr 27
format binary as "mnt"
28
 
2288 clevermous 29
include 'macros.inc'
2381 hidnplayr 30
include 'struct.inc'
2288 clevermous 31
 
32
$Revision: 9958 $
33
 
34
 
7136 dunkaist 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
2288 clevermous 37
 
38
; Enabling the next line will enable serial output console
9958 dunkaist 39
;debug_com_base  = 0x3f8  ; 0x3f8 is com1, 0x2f8 is com2, 0x3e8 is com3,
40
                          ; 0x2e8 is com4, no irq's are used
2288 clevermous 41
 
42
include "proc32.inc"
43
include "kglobals.inc"
44
include "lang.inc"
3539 clevermous 45
include "encoding.inc"
2288 clevermous 46
 
47
include "const.inc"
3777 yogev_ezra 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
 
7136 dunkaist 56
max_processes  =  255
57
tss_step       =  128 + 8192 ; tss & i/o - 65535 ports, * 256=557056*4
2288 clevermous 58
 
7136 dunkaist 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
2288 clevermous 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
 
7129 dunkaist 93
; That is a loading and initialization code that also draws the blue screen
9958 dunkaist 94
; menu with svn revision number near top right corner of the screen.
95
include "bootbios.inc"
2288 clevermous 96
 
97
use32
9958 dunkaist 98
org $+KERNEL_BASE
2288 clevermous 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
6244 serge 108
        mov     esp, TMP_STACK_TOP       ; Set stack
2288 clevermous 109
 
110
; CLEAR 0x280000 - HEAP_BASE
111
 
112
        xor     eax, eax
113
        mov     edi, CLEAN_ZONE
9715 Doczom 114
        mov     ecx, (HEAP_BASE - OS_BASE - CLEAN_ZONE) / 4
2288 clevermous 115
        cld
116
        rep stosd
117
 
118
; CLEAR KERNEL UNDEFINED GLOBALS
9715 Doczom 119
        mov     edi, endofcode - OS_BASE
2288 clevermous 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
3732 Serge 128
        mov     ecx, 0x8000 / 4
2288 clevermous 129
        rep stosd
3732 Serge 130
        mov     edi, 0xa000
131
        mov     ecx, 0x6000 / 4
132
        rep stosd
2288 clevermous 133
 
134
        call    test_cpu
9715 Doczom 135
        bts     [cpu_caps - OS_BASE], CAPS_TSC    ;force use rdtsc
2288 clevermous 136
 
8111 dunkaist 137
        call    acpi_locate
2288 clevermous 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
 
9715 Doczom 146
        mov     eax, sys_proc - OS_BASE + PROC.pdt_0
2288 clevermous 147
        mov     cr3, eax
148
 
149
        mov     eax, cr0
9715 Doczom 150
        or      eax, CR0_PG + CR0_WP
2288 clevermous 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
5033 clevermous 160
ap_init16:
161
        cli
9715 Doczom 162
        lgdt    [cs:gdts_ap - ap_init16]
163
        mov     eax, [cs:cr3_ap - ap_init16]
5033 clevermous 164
        mov     cr3, eax
9715 Doczom 165
        mov     eax, [cs:cr4_ap - ap_init16]
5033 clevermous 166
        mov     cr4, eax
9715 Doczom 167
        mov     eax, CR0_PE + CR0_PG + CR0_WP
5033 clevermous 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
2288 clevermous 178
use32
179
 
180
__DEBUG__ fix 1
181
__DEBUG_LEVEL__ fix 1
182
include 'init.inc'
183
 
184
org OS_BASE+$
185
 
2443 Serge 186
include 'fdo.inc'
187
 
2288 clevermous 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
 
5360 serge 201
        xor     eax, eax
9715 Doczom 202
        mov     ebx, 0xFFFFF000 + PG_SHARED + PG_NOCACHE + PG_UWR
5360 serge 203
        bt      [cpu_caps], CAPS_PAT
204
        setc    al
205
        shl     eax, 7
206
        or      ebx, eax
207
 
5359 serge 208
        mov     eax, PG_GLOBAL
2288 clevermous 209
        bt      [cpu_caps], CAPS_PGE
210
        jnc     @F
211
 
9715 Doczom 212
        or      [sys_proc + PROC.pdt_0 + (OS_BASE shr 20)], eax
5360 serge 213
        or      ebx, eax
2288 clevermous 214
 
5360 serge 215
        mov     eax, cr4
216
        or      eax, CR4_PGE
217
        mov     cr4, eax
2288 clevermous 218
@@:
5360 serge 219
        mov     [pte_valid_mask], ebx
220
 
2288 clevermous 221
        xor     eax, eax
9715 Doczom 222
        mov     dword [sys_proc + PROC.pdt_0], eax
223
        mov     dword [sys_proc + PROC.pdt_0+4], eax
2288 clevermous 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
 
2601 clevermous 234
        mov     ecx, keyboard_list_mutex
235
        call    mutex_init
236
 
2489 mario79 237
        mov     ecx, unpack_mutex
238
        call    mutex_init
239
 
3534 clevermous 240
        mov     ecx, application_table_mutex
241
        call    mutex_init
242
 
3742 clevermous 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
4700 mario79 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
3774 mario79 257
;-----------------------------------------------------------------------------
2288 clevermous 258
; SAVE REAL MODE VARIABLES
3774 mario79 259
;-----------------------------------------------------------------------------
2288 clevermous 260
; --------------- APM ---------------------
261
 
262
; init selectors
7132 dunkaist 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
2288 clevermous 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
 
7132 dunkaist 289
        mov     eax, dword[BOOT.apm_version]   ; version & flags
2288 clevermous 290
        mov     [apm_vf], eax
291
; -----------------------------------------
7132 dunkaist 292
        mov     al, [BOOT.dma]            ; DMA access
2288 clevermous 293
        mov     [allow_dma_access], al
6263 serge 294
 
7132 dunkaist 295
        mov     al, [BOOT.debug_print]    ; If nonzero, duplicates debug output to the screen
3777 yogev_ezra 296
        mov     [debug_direct_print], al
6263 serge 297
 
7132 dunkaist 298
        mov     al, [BOOT.launcher_start] ; Start the first app (LAUNCHER) after kernel is loaded?
3777 yogev_ezra 299
        mov     [launcher_start], al
2288 clevermous 300
 
8092 dunkaist 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
 
7132 dunkaist 306
        mov     esi, BOOT.bios_hd
2288 clevermous 307
        movzx   ecx, byte [esi-1]
308
        mov     [NumBiosDisks], ecx
309
        mov     edi, BiosDisksData
6843 dunkaist 310
        shl     ecx, 2
2288 clevermous 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 :
3539 clevermous 343
        ; Bit 31–16 During the SYSRET instruction, this field is copied into the CS register
2288 clevermous 344
        ;  and the contents of this field, plus 8, are copied into the SS register.
3539 clevermous 345
        ; Bit 15–0 During the SYSCALL instruction, this field is copied into the CS register
2288 clevermous 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
5356 serge 357
        stdcall map_page, tss-0xF80, eax, PG_SWR
2288 clevermous 358
        stdcall alloc_page
5356 serge 359
        stdcall map_page, tss+0x80, eax, PG_SWR
2288 clevermous 360
        stdcall alloc_page
5356 serge 361
        stdcall map_page, tss+0x1080, eax, PG_SWR
2288 clevermous 362
 
363
; LOAD IDT
364
 
365
        call    build_interrupt_table ;lidt is executed
366
          ;lidt [idtreg]
367
 
368
        call    init_kernel_heap
7276 dunkaist 369
        call    init_fpu
370
        mov     eax, [xsave_area_size]
371
        lea     eax, [eax*2 + RING0_STACK_SIZE*2]
372
        stdcall kernel_alloc, eax
2288 clevermous 373
        mov     [os_stack_seg], eax
374
 
9715 Doczom 375
        lea     esp, [eax + RING0_STACK_SIZE]
2288 clevermous 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
 
6263 serge 397
        mov     eax, sys_proc
398
        list_init eax
399
        add     eax, PROC.thr_list
400
        list_init eax
401
 
402
        call    init_video
9950 turbocat 403
        call    init_pat_mtrr
6263 serge 404
        mov     [LFBAddress], LFB_BASE
405
        mov     ecx, bios_fb
406
        call    set_framebuffer
2288 clevermous 407
        call    init_malloc
408
 
5130 serge 409
        stdcall alloc_kernel_space, 0x50000         ; FIXME check size
2288 clevermous 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
 
9715 Doczom 434
        stdcall kernel_alloc, (unpack.LZMA_BASE_SIZE + (unpack.LZMA_LIT_SIZE shl \
435
                (unpack.lc + unpack.lp)))*4
2288 clevermous 436
 
437
        mov     [unpack.p], eax
438
 
439
        call    init_events
9715 Doczom 440
        mov     eax, srv.fd - SRV.fd
2288 clevermous 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
 
5351 serge 451
        stdcall kernel_alloc, [_display.win_map_size]
452
        mov     [_display.win_map], eax
2288 clevermous 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
 
4199 mario79 465
; set clipboard
466
 
467
        xor     eax, eax
468
        mov     [clipboard_slots], eax
469
        mov     [clipboard_write_lock], eax
9926 Doczom 470
        stdcall kernel_alloc, PAGE_SIZE
4199 mario79 471
        test    eax, eax
472
        jnz     @f
473
 
474
        dec     eax
475
@@:
476
        mov     [clipboard_main_list], eax
477
 
8111 dunkaist 478
        call    check_acpi
479
 
5787 serge 480
        mov     eax, [hpet_base]
481
        test    eax, eax
482
        jz      @F
9715 Doczom 483
        stdcall map_io_mem, [hpet_base], 1024, PG_GLOBAL + PAT_UC + PG_SWR
5787 serge 484
        mov     [hpet_base], eax
8111 dunkaist 485
        mov     eax, [eax+HPET_ID]
5787 serge 486
        DEBUGF  1, "K : HPET caps %x\n", eax
8111 dunkaist 487
        call    init_hpet
5787 serge 488
@@:
3296 clevermous 489
; SET UP OS TASK
490
 
491
        mov     esi, boot_setostask
492
        call    boot_log
493
 
9715 Doczom 494
        mov     edi, sys_proc + PROC.heap_lock
495
        mov     ecx, (PROC.ht_free - PROC.heap_lock)/4
5130 serge 496
 
5595 serge 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
 
9715 Doczom 512
        mov     [sys_proc + PROC.pdt_0_phys], sys_proc - OS_BASE + PROC.pdt_0
5595 serge 513
 
5130 serge 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
 
9715 Doczom 527
        mov     edx, SLOT_BASE + sizeof.APPDATA*1
3534 clevermous 528
        mov     ebx, [os_stack_seg]
7276 dunkaist 529
        add     ebx, RING0_STACK_SIZE
530
        add     ebx, [xsave_area_size]
3534 clevermous 531
        call    setup_os_slot
532
        mov     dword [edx], 'IDLE'
9715 Doczom 533
        sub     [edx + APPDATA.saved_esp], 4
534
        mov     eax, [edx + APPDATA.saved_esp]
3534 clevermous 535
        mov     dword [eax], idle_thread
536
        mov     ecx, IDLE_PRIORITY
537
        call    scheduler_add_thread
3296 clevermous 538
 
9715 Doczom 539
        mov     edx, SLOT_BASE + sizeof.APPDATA*2
3534 clevermous 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
3296 clevermous 545
 
8869 rgimad 546
        mov     dword [current_slot_idx], 2
8867 rgimad 547
        mov     [thread_count], 2
8093 dunkaist 548
        mov     dword [current_slot], SLOT_BASE + sizeof.APPDATA*2
3296 clevermous 549
 
5033 clevermous 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
9715 Doczom 564
        mov     [cr3_ap + OS_BASE], eax
5033 clevermous 565
        mov     eax, cr4
9715 Doczom 566
        mov     [cr4_ap + OS_BASE], eax
5033 clevermous 567
        mov     esi, OS_BASE + ap_init16
568
        mov     edi, OS_BASE + 8000h
569
        mov     ecx, (ap_init16_size + 3) / 4
570
        rep movsd
8119 dunkaist 571
        mov     eax, [LAPIC_BASE]
572
        test    eax, eax
573
        jnz     @f
9715 Doczom 574
        stdcall map_io_mem, [acpi_lapic_base], 0x1000, PG_GLOBAL + PG_NOCACHE + PG_SWR
5033 clevermous 575
        mov     [LAPIC_BASE], eax
8119 dunkaist 576
@@:
9715 Doczom 577
        lea     edi, [eax + APIC_ICRL]
5033 clevermous 578
        mov     esi, smpt+4
579
        dec     ebx
580
.wake_cpus_loop:
581
        lodsd
5049 clevermous 582
        push    esi
583
        xor     esi, esi
584
        inc     esi
5033 clevermous 585
        shl     eax, 24
586
        mov     [edi+10h], eax
587
; assert INIT IPI
588
        mov     dword [edi], 0C500h
5049 clevermous 589
        call    delay_ms
5033 clevermous 590
@@:
591
        test    dword [edi], 1000h
592
        jnz     @b
593
; deassert INIT IPI
594
        mov     dword [edi], 8500h
5049 clevermous 595
        call    delay_ms
5033 clevermous 596
@@:
597
        test    dword [edi], 1000h
598
        jnz     @b
599
; send STARTUP IPI
600
        mov     dword [edi], 600h + (8000h shr 12)
5049 clevermous 601
        call    delay_ms
5033 clevermous 602
@@:
603
        test    dword [edi], 1000h
604
        jnz     @b
5049 clevermous 605
        pop     esi
5033 clevermous 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
9715 Doczom 613
        mov     eax, [cr3_ap + OS_BASE]
5033 clevermous 614
        call    free_page
615
.no_wake_cpus:
2288 clevermous 616
 
617
; REDIRECT ALL IRQ'S TO INT'S 0x20-0x2f
2642 mario79 618
        mov     esi, boot_initirq
619
        call    boot_log
620
        call    init_irqs
2288 clevermous 621
 
2642 mario79 622
        mov     esi, boot_picinit
623
        call    boot_log
2288 clevermous 624
        call    PIC_init
625
 
2642 mario79 626
        mov     esi, boot_v86machine
627
        call    boot_log
2288 clevermous 628
; Initialize system V86 machine
629
        call    init_sys_v86
630
 
2642 mario79 631
        mov     esi, boot_inittimer
632
        call    boot_log
2288 clevermous 633
; Initialize system timer (IRQ0)
634
        call    PIT_init
635
 
4273 clevermous 636
; Register ramdisk file system
8091 dunkaist 637
if ~ defined extended_primary_loader
8089 dunkaist 638
        cmp     [BOOT.rd_load_from], RD_LOAD_FROM_HD    ; will be loaded later
4772 mario79 639
        je      @f
8091 dunkaist 640
end if
8089 dunkaist 641
        cmp     [BOOT.rd_load_from], RD_LOAD_FROM_NONE
642
        je      @f
643
        call    register_ramdisk
4772 mario79 644
;--------------------------------------
645
@@:
2642 mario79 646
        mov     esi, boot_initapic
647
        call    boot_log
2288 clevermous 648
; Try to Initialize APIC
649
        call    APIC_init
650
 
2642 mario79 651
        mov     esi, boot_enableirq
652
        call    boot_log
3785 mario79 653
; Enable timer IRQ (IRQ0) and co-processor IRQ (IRQ13)
2288 clevermous 654
; they are used: when partitions are scanned, hd_read relies on timer
655
        call    unmask_timer
7733 dunkaist 656
        ; Prevent duplicate timer IRQs in APIC mode
657
        cmp     [irq_mode], IRQ_APIC
658
        jz      @f
2288 clevermous 659
        stdcall enable_irq, 2               ; @#$%! PIC
7733 dunkaist 660
@@:
2288 clevermous 661
        stdcall enable_irq, 13              ; co-processor
4741 hidnplayr 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
 
4575 mario79 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
;-----------------------------------------------------------------------------
4624 mario79 725
; create pci-devices list
4575 mario79 726
;-----------------------------------------------------------------------------
4624 mario79 727
        mov     [pci_access_enabled], 1
728
        call    pci_enum
3762 mario79 729
;-----------------------------------------------------------------------------
4624 mario79 730
; initialisation IDE ATA code
4575 mario79 731
;-----------------------------------------------------------------------------
4624 mario79 732
include 'detect/init_ata.inc'
4575 mario79 733
;-----------------------------------------------------------------------------
9183 rgimad 734
; initialisation AHCI code
735
;-----------------------------------------------------------------------------
736
        jmp     ahci_code_end
737
include 'blkdev/ahci.inc'
738
ahci_code_end:
739
        call    ahci_init
740
;-----------------------------------------------------------------------------
2288 clevermous 741
if 0
8089 dunkaist 742
        mov     ax, [BOOT.sys_disk]
2288 clevermous 743
        cmp     ax, 'r1'; if using not ram disk, then load librares and parameters {SPraid.simba}
744
        je      no_lib_load
2642 mario79 745
 
746
        mov     esi, boot_loadlibs
747
        call    boot_log
2288 clevermous 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
@@:
2642 mario79 761
        call    boot_log
2288 clevermous 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
3274 esevece 770
        else if lang eq sp
771
        or      ecx, (10+33*6) shl 16
772
        else
2288 clevermous 773
        or      ecx, (10+29*6) shl 16
3274 esevece 774
        end if
2288 clevermous 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
 
2642 mario79 786
;        call    build_scheduler; sys32.inc
2288 clevermous 787
 
2642 mario79 788
;        mov     esi, boot_devices
789
;        call    boot_log
2288 clevermous 790
 
4297 yogev_ezra 791
include "detect/vortex86.inc"                     ; Vortex86 SoC detection code
792
 
5039 clevermous 793
        stdcall load_pe_driver, szVidintel, 0
3508 clevermous 794
 
3520 clevermous 795
        call    usb_init
796
 
2288 clevermous 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
 
9930 Doczom 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
 
2288 clevermous 823
        call    init_display
824
        mov     eax, [def_cursor]
9930 Doczom 825
        mov     [window_data + sizeof.WDATA + WDATA.cursor], eax
826
        mov     [window_data + sizeof.WDATA*2 + WDATA.cursor], eax
2288 clevermous 827
 
3501 Serge 828
; PRINT CPU FREQUENCY
2288 clevermous 829
 
3501 Serge 830
        mov     esi, boot_cpufreq
2288 clevermous 831
        call    boot_log
3501 Serge 832
 
5792 serge 833
        cli
834
        mov     ebx, [hpet_base]
835
        test    ebx, ebx
836
        jz      @F
9715 Doczom 837
        mov     ebx, [ebx + HPET_COUNTER]
5792 serge 838
 
3501 Serge 839
        rdtsc
5792 serge 840
        mov     ecx, 1000
841
        sub     eax, [hpet_tsc_start]
9715 Doczom 842
        sbb     edx, [hpet_tsc_start + 4]
5792 serge 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
2288 clevermous 859
        mov     ecx, eax
860
        mov     esi, 250            ; wait 1/4 a second
861
        call    delay_ms
3501 Serge 862
        rdtsc
863
 
2288 clevermous 864
        sub     eax, ecx
3481 Serge 865
        xor     edx, edx
866
        shld    edx, eax, 2
2288 clevermous 867
        shl     eax, 2
5792 serge 868
.next:
3481 Serge 869
        mov     dword [cpu_freq], eax
9715 Doczom 870
        mov     dword [cpu_freq + 4], edx
3502 Serge 871
        mov     ebx, 1000000
872
        div     ebx
3501 Serge 873
        mov     ebx, eax
2288 clevermous 874
 
875
        movzx   ecx, word [boot_y]
876
        if lang eq ru
3501 Serge 877
        add     ecx, (10+19*6) shl 16 - 10
3274 esevece 878
        else if lang eq sp
3501 Serge 879
        add     ecx, (10+25*6) shl 16 - 10
3274 esevece 880
        else
3501 Serge 881
        add     ecx, (10+17*6) shl 16 - 10
3274 esevece 882
        end if
3501 Serge 883
 
2288 clevermous 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
 
5635 hidnplayr 919
; Protect I/O permission map
2288 clevermous 920
 
921
        mov     esi, [default_io_map]
9715 Doczom 922
        stdcall map_page, esi, [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map], PG_READ
2288 clevermous 923
        add     esi, 0x1000
9715 Doczom 924
        stdcall map_page, esi, [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map + 4], PG_READ
2288 clevermous 925
 
926
        stdcall map_page, tss._io_map_0, \
9715 Doczom 927
                [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map], PG_READ
2288 clevermous 928
        stdcall map_page, tss._io_map_1, \
9715 Doczom 929
                [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map + 4], PG_READ
2288 clevermous 930
 
931
; SET KEYBOARD PARAMETERS
932
        mov     al, 0xf6       ; reset keyboard, scan enabled
5012 clevermous 933
        call    kb_write_wait_ack
2601 clevermous 934
        test    ah, ah
935
        jnz     .no_keyboard
2288 clevermous 936
 
2601 clevermous 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
2288 clevermous 946
       ; mov   al, 0xED       ; Keyboard LEDs - only for testing!
5012 clevermous 947
       ; call  kb_write_wait_ack
2288 clevermous 948
       ; mov   al, 111b
5012 clevermous 949
       ; call  kb_write_wait_ack
2288 clevermous 950
 
951
        mov     al, 0xF3     ; set repeat rate & delay
5012 clevermous 952
        call    kb_write_wait_ack
2288 clevermous 953
        mov     al, 0; 30 250 ;00100010b ; 24 500  ;00100100b  ; 20 500
5012 clevermous 954
        call    kb_write_wait_ack
2288 clevermous 955
     ;// mike.dld [
956
        call    set_lights
957
     ;// mike.dld ]
958
        stdcall attach_int_handler, 1, irq1, 0
4734 mario79 959
        DEBUGF  1, "K : IRQ1 return code %x\n", eax
2601 clevermous 960
.no_keyboard:
2288 clevermous 961
 
4553 hidnplayr 962
; Load PS/2 mouse driver
2288 clevermous 963
        mov     esi, boot_setmouse
964
        call    boot_log
9514 rgimad 965
        stdcall load_pe_driver, szPS2MDriver, 0
2288 clevermous 966
 
5031 clevermous 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
 
2288 clevermous 985
; START MULTITASKING
7129 dunkaist 986
preboot_blogesc = 0       ; start immediately after bootlog
2288 clevermous 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
4734 mario79 1000
 
4608 clevermous 1001
        call    mtrr_validate
1002
 
2288 clevermous 1003
        jmp     osloop
1004
        ; Fly :)
1005
 
5033 clevermous 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
9715 Doczom 1021
        mov     eax, sys_proc - OS_BASE + PROC.pdt_0
5033 clevermous 1022
        mov     cr3, eax
1023
        lock inc [ap_initialized]
1024
        jmp     idle_loop
1025
 
1026
 
2288 clevermous 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
 
8091 dunkaist 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
 
3534 clevermous 1057
; in: edx -> APPDATA for OS/IDLE slot
1058
; in: ebx = stack base
1059
proc setup_os_slot
1060
        xor     eax, eax
8093 dunkaist 1061
        mov     ecx, sizeof.APPDATA/4
3534 clevermous 1062
        mov     edi, edx
1063
        rep stosd
1064
 
3535 clevermous 1065
        mov     eax, tss+0x80
1066
        call    get_pg_addr
1067
        inc     eax
9715 Doczom 1068
        mov     [edx + APPDATA.io_map], eax
3535 clevermous 1069
        mov     eax, tss+0x1080
1070
        call    get_pg_addr
1071
        inc     eax
9715 Doczom 1072
        mov     [edx + APPDATA.io_map + 4], eax
3535 clevermous 1073
 
9715 Doczom 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
3534 clevermous 1080
 
1081
        mov     esi, fpu_data
7165 clevermous 1082
        mov     ecx, [xsave_area_size]
1083
        add     ecx, 3
1084
        shr     ecx, 2
3534 clevermous 1085
        rep movsd
1086
 
9715 Doczom 1087
        lea     eax, [edx + APP_EV_OFFSET]
1088
        mov     dword [edx + APPDATA.fd_ev], eax
1089
        mov     dword [edx + APPDATA.bk_ev], eax
8087 dunkaist 1090
 
9715 Doczom 1091
        lea     eax, [edx + APP_OBJ_OFFSET]
1092
        mov     dword [edx + APPDATA.fd_obj], eax
1093
        mov     dword [edx + APPDATA.bk_obj], eax
3534 clevermous 1094
 
9715 Doczom 1095
        mov     dword [edx + APPDATA.cur_dir], sysdir_path-2
3534 clevermous 1096
 
5130 serge 1097
        mov     [edx + APPDATA.process], sys_proc
3534 clevermous 1098
 
9715 Doczom 1099
        lea     ebx, [edx + APPDATA.list]
1100
        lea     ecx, [sys_proc + PROC.thr_list]
5130 serge 1101
        list_add_tail ebx, ecx
1102
 
9715 Doczom 1103
        mov     [edx + APPDATA.wnd_number], dh
1104
        mov     byte [edx + APPDATA.tid], dh
9930 Doczom 1105
        movzx   eax, dh
1106
        shl     eax, BSF sizeof.WDATA
1107
        add     eax, window_data
1108
        mov     [edx + APPDATA.window], eax
3534 clevermous 1109
 
1110
        ret
1111
endp
1112
 
2288 clevermous 1113
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1114
;                                                                    ;
1115
;                    MAIN OS LOOP START                              ;
1116
;                                                                    ;
1117
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1118
align 32
1119
osloop:
3534 clevermous 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
4734 mario79 1127
 
2453 mario79 1128
        call    __sys_draw_pointer
2288 clevermous 1129
        call    window_check_events
1130
        call    mouse_check_events
1131
        call    checkmisc
1132
        call    checkVga_N13
4734 mario79 1133
;--------------------------------------
3534 clevermous 1134
.no_periodic:
2288 clevermous 1135
        call    stack_handler
1136
        call    check_fdd_motor_status
1137
        call    check_ATAPI_device_event
2601 clevermous 1138
        call    check_lights_state
2288 clevermous 1139
        call    check_timers
4734 mario79 1140
 
2288 clevermous 1141
        jmp     osloop
1142
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1143
;                                                                    ;
1144
;                      MAIN OS LOOP END                              ;
1145
;                                                                    ;
1146
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3534 clevermous 1147
proc osloop_has_work?
1148
        cmp     [osloop_nonperiodic_work], 0
1149
        jnz     .yes
1150
        call    stack_handler_has_work?
1151
        jnz     .yes
4273 clevermous 1152
        call    check_fdd_motor_status_has_work?
1153
        jnz     .yes
3534 clevermous 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
2288 clevermous 1162
        ret
3534 clevermous 1163
.yes:
1164
        xor     eax, eax
1165
        inc     eax
1166
        ret
1167
endp
2288 clevermous 1168
 
3534 clevermous 1169
proc wakeup_osloop
1170
        mov     [osloop_nonperiodic_work], 1
1171
        ret
1172
endp
1173
 
2288 clevermous 1174
uglobal
3534 clevermous 1175
align 4
1176
osloop_nonperiodic_work dd      ?
2288 clevermous 1177
endg
1178
 
5033 clevermous 1179
uglobal
1180
align 64
1181
idle_addr       rb      64
1182
endg
1183
 
3534 clevermous 1184
idle_thread:
1185
        sti
5033 clevermous 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.
3534 clevermous 1191
idle_loop:
5033 clevermous 1192
        cmp     [use_mwait_for_idle], 0
9911 Doczom 1193
        jnz     .mwait
5033 clevermous 1194
 
9911 Doczom 1195
.hlt:
3534 clevermous 1196
        hlt
9911 Doczom 1197
        jmp     .hlt
2288 clevermous 1198
 
9911 Doczom 1199
.mwait:
5033 clevermous 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
9911 Doczom 1207
        jmp     .mwait
2288 clevermous 1208
 
3534 clevermous 1209
 
2288 clevermous 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
3426 hidnplayr 1237
        mov     [eax+16+8], dword 0x2D
2288 clevermous 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
3426 hidnplayr 1245
        mov     [eax+48+8], dword 0xDF
2288 clevermous 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
3534 clevermous 1255
  process_number dd 0x2
2288 clevermous 1256
endg
1257
 
1258
set_variables:
1259
 
1260
        mov     ecx, 0x16                    ; flush port 0x60
9911 Doczom 1261
@@:
2288 clevermous 1262
        in      al, 0x60
9911 Doczom 1263
        loop    @b
2288 clevermous 1264
        push    eax
1265
 
7132 dunkaist 1266
        mov     ax, [BOOT.y_res]
2288 clevermous 1267
        shr     ax, 1
1268
        shl     eax, 16
7132 dunkaist 1269
        mov     ax, [BOOT.x_res]
2288 clevermous 1270
        shr     ax, 1
1271
        mov     [MOUSE_X], eax
3534 clevermous 1272
        call    wakeup_osloop
2288 clevermous 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
9488 rgimad 1282
;-----------------------------------------------------------------------------
2288 clevermous 1283
 
1284
align 4
1285
display_number:
8713 Doczom 1286
; add check pointers
1287
        test    bl, bl
1288
        jz      @f
9910 Doczom 1289
        bt      ebx, 30  ; check 30 bit
1290
        jb      @f
1291
        stdcall is_region_userspace, ecx, 4
9045 dunkaist 1292
        jz      @f
8713 Doczom 1293
        ret
1294
@@:
9910 Doczom 1295
        jz      @f
1296
        stdcall is_region_userspace, ecx, 8
1297
        jz      @f
1298
        ret
1299
@@:
8713 Doczom 1300
        test    esi, 0x08000000
1301
        jz      @f
1302
        stdcall is_region_userspace, edi, 1
9045 dunkaist 1303
        jz      @f
8713 Doczom 1304
        ret
1305
@@:
2288 clevermous 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
9911 Doczom 1329
        jge     .cont_displ
2288 clevermous 1330
        ret
9911 Doczom 1331
   .cont_displ:
2288 clevermous 1332
        push    eax
1333
        and     eax, 0x3fffffff
1334
        cmp     eax, 61*0x10000  ; length <= 60 ?
1335
        pop     eax
9911 Doczom 1336
        jb      .cont_displ2
2288 clevermous 1337
        ret
9911 Doczom 1338
   .cont_displ2:
2288 clevermous 1339
 
1340
        pushad
1341
 
1342
        cmp     al, 1            ; ecx is a pointer ?
9911 Doczom 1343
        jne     @f
2288 clevermous 1344
        mov     ebp, ebx
1345
        add     ebp, 4
9911 Doczom 1346
        mov     ebp, [ebp] ;[ebp + std_application_base_address]
1347
        mov     ebx, [ebx] ;[ebx + std_application_base_address]
1348
@@:
2288 clevermous 1349
        sub     esp, 64
1350
 
1351
        test    ah, ah            ; DECIMAL
9911 Doczom 1352
        jnz     .no_display_desnum
1353
 
2288 clevermous 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
9911 Doczom 1364
@@:
2288 clevermous 1365
        xor     edx, edx
1366
        call    division_64_bits
1367
        div     ebx
1368
        add     dl, 48
1369
        mov     [edi], dl
1370
        dec     edi
9911 Doczom 1371
        loop    @b
1372
 
2288 clevermous 1373
        pop     eax
1374
        call    normalize_number
1375
        call    draw_num_text
1376
        add     esp, 64
1377
        popad
1378
        ret
9911 Doczom 1379
.no_display_desnum:
2288 clevermous 1380
 
1381
        cmp     ah, 0x01         ; HEXADECIMAL
9911 Doczom 1382
        jne     .no_display_hexnum
1383
 
2288 clevermous 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
9911 Doczom 1394
@@:
2288 clevermous 1395
        xor     edx, edx
1396
        call    division_64_bits
1397
        div     ebx
9911 Doczom 1398
   ;hexletters = __fdo_hexdigits
1399
        add     edx, __fdo_hexdigits ;hexletters
2288 clevermous 1400
        mov     dl, [edx]
1401
        mov     [edi], dl
1402
        dec     edi
9911 Doczom 1403
        loop    @b
1404
 
2288 clevermous 1405
        pop     eax
1406
        call    normalize_number
1407
        call    draw_num_text
1408
        add     esp, 64
1409
        popad
1410
        ret
9911 Doczom 1411
.no_display_hexnum:
2288 clevermous 1412
 
1413
        cmp     ah, 0x02         ; BINARY
9911 Doczom 1414
        jne     .no_display_binnum
1415
 
2288 clevermous 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
9911 Doczom 1426
@@:
2288 clevermous 1427
        xor     edx, edx
1428
        call    division_64_bits
1429
        div     ebx
1430
        add     dl, 48
1431
        mov     [edi], dl
1432
        dec     edi
9911 Doczom 1433
        loop    @b
1434
 
2288 clevermous 1435
        pop     eax
1436
        call    normalize_number
1437
        call    draw_num_text
1438
        add     esp, 64
1439
        popad
1440
        ret
9911 Doczom 1441
.no_display_binnum:
2288 clevermous 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
 
9930 Doczom 1483
        mov     ecx, [current_slot]
1484
        mov     ecx, [ecx + APPDATA.window]
2288 clevermous 1485
 
9930 Doczom 1486
        mov     eax, [ecx + WDATA.box.left]
1487
        add     eax, [ecx + WDATA.clientbox.left]
2288 clevermous 1488
        shl     eax, 16
9930 Doczom 1489
        add     eax, [ecx + WDATA.box.top]
1490
        add     eax, [ecx + WDATA.clientbox.top]
2288 clevermous 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]
6272 pathoswith 1495
        and     ecx, 5FFFFFFFh
1496
        bt      ecx, 27
1497
        jnc     @f
1498
        mov     edi, eax
1499
@@:
2288 clevermous 1500
        jmp     dtext
4700 mario79 1501
;-----------------------------------------------------------------------------
2288 clevermous 1502
align 4
1503
sys_setup:
4700 mario79 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
2288 clevermous 1514
; 11 = enable lba read
1515
; 12 = enable pci access
4700 mario79 1516
;-----------------------------------------------------------------------------
9831 dunkaist 1517
        and     [esp + SYSCALL_STACK.eax], dword 0
4700 mario79 1518
; F.21.1 - set MPU MIDI base port
1519
        dec     ebx
1520
        jnz     @f
2288 clevermous 1521
 
1522
        cmp     ecx, 0x100
4700 mario79 1523
        jb      @f
2288 clevermous 1524
 
1525
        mov     esi, 65535
1526
        cmp     esi, ecx
4700 mario79 1527
        jb      @f
2288 clevermous 1528
 
4700 mario79 1529
        mov     [midi_base], cx
1530
        mov     word [mididp], cx
1531
        inc     cx
1532
        mov     word [midisp], cx
2288 clevermous 1533
        ret
4700 mario79 1534
;--------------------------------------
1535
@@:
1536
; F.21.2 - set keyboard layout
1537
        dec     ebx
1538
        jnz     @f
2288 clevermous 1539
 
9710 Doczom 1540
        mov     eax, edx
4700 mario79 1541
; 1 = normal layout
1542
        dec     ecx
1543
        jnz     .shift
2288 clevermous 1544
 
1545
        mov     ebx, keymap
1546
        mov     ecx, 128
1547
        call    memmove
1548
        ret
4700 mario79 1549
;--------------------------------------
1550
.shift:
1551
; 2 = layout at pressed Shift
2288 clevermous 1552
        dec     ecx
4700 mario79 1553
        jnz     .alt
2288 clevermous 1554
 
1555
        mov     ebx, keymap_shift
1556
        mov     ecx, 128
1557
        call    memmove
1558
        ret
4700 mario79 1559
;--------------------------------------
1560
.alt:
1561
; 3 = layout at pressed Alt
2288 clevermous 1562
        dec     ecx
4700 mario79 1563
        jnz     .country
1564
 
2288 clevermous 1565
        mov     ebx, keymap_alt
1566
        mov     ecx, 128
1567
        call    memmove
1568
        ret
4700 mario79 1569
;--------------------------------------
1570
.country:
1571
; country identifier
2288 clevermous 1572
        sub     ecx, 6
4700 mario79 1573
        jnz     .error
1574
 
2288 clevermous 1575
        mov     word [keyboard], dx
1576
        ret
4700 mario79 1577
;--------------------------------------
1578
@@:
1579
; F.21.5 - set system language
1580
        sub     ebx, 3
1581
        jnz     @f
2288 clevermous 1582
 
1583
        mov     [syslang], ecx
1584
        ret
4700 mario79 1585
;--------------------------------------
1586
@@:
1587
; F.21.11 - enable/disable low-level access to HD
1588
        and     ecx, 1
1589
        sub     ebx, 6
1590
        jnz     @f
2288 clevermous 1591
 
4700 mario79 1592
        mov     [lba_read_enabled], ecx
2288 clevermous 1593
        ret
4700 mario79 1594
;--------------------------------------
1595
@@:
1596
; F.21.12 - enable/disable low-level access to PCI
2288 clevermous 1597
        dec     ebx
4700 mario79 1598
        jnz     .error
2288 clevermous 1599
 
1600
        mov     [pci_access_enabled], ecx
1601
        ret
4700 mario79 1602
;--------------------------------------
1603
.error:
9831 dunkaist 1604
        or      [esp + SYSCALL_STACK.eax], dword -1
2288 clevermous 1605
        ret
4700 mario79 1606
;-----------------------------------------------------------------------------
2288 clevermous 1607
align 4
1608
sys_getsetup:
4700 mario79 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
2288 clevermous 1625
 
1626
        movzx   eax, [midi_base]
9831 dunkaist 1627
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 1628
        ret
4700 mario79 1629
;--------------------------------------
1630
@@:
1631
; F.26.2 - get keyboard layout
2288 clevermous 1632
        dec     ebx
4700 mario79 1633
        jnz     @f
2288 clevermous 1634
 
9710 Doczom 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
4700 mario79 1639
; 1 = normal layout
1640
        dec     ecx
1641
        jnz     .shift
2288 clevermous 1642
 
1643
        mov     eax, keymap
1644
        mov     ecx, 128
1645
        call    memmove
1646
        ret
4700 mario79 1647
;--------------------------------------
1648
.shift:
1649
; 2 = layout with pressed Shift
2288 clevermous 1650
        dec     ecx
4700 mario79 1651
        jnz     .alt
2288 clevermous 1652
 
1653
        mov     eax, keymap_shift
1654
        mov     ecx, 128
1655
        call    memmove
1656
        ret
4700 mario79 1657
;--------------------------------------
1658
.alt:
1659
; 3 = layout with pressed Alt
2288 clevermous 1660
        dec     ecx
4700 mario79 1661
        jne     .country
2288 clevermous 1662
 
1663
        mov     eax, keymap_alt
1664
        mov     ecx, 128
1665
        call    memmove
1666
        ret
4700 mario79 1667
;--------------------------------------
1668
.country:
1669
; 9 = country identifier
2288 clevermous 1670
        sub     ecx, 6
4700 mario79 1671
        jnz     .error
1672
 
2288 clevermous 1673
        movzx   eax, word [keyboard]
9831 dunkaist 1674
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 1675
        ret
8158 rgimad 1676
 
1677
.addr_error:    ; if given memory address is illegal
9831 dunkaist 1678
        or      dword [esp + SYSCALL_STACK.eax], -1
9947 ace_dent 1679
        ret
4700 mario79 1680
;--------------------------------------
1681
@@:
1682
; F.26.5 - get system language
1683
        sub     ebx, 3
1684
        jnz     @f
2288 clevermous 1685
 
1686
        mov     eax, [syslang]
9831 dunkaist 1687
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 1688
        ret
4700 mario79 1689
;--------------------------------------
1690
@@:
1691
; F.26.9 - get the value of the time counter
4573 clevermous 1692
        sub     ebx, 4
4700 mario79 1693
        jnz     @f
1694
 
1695
        mov     eax, [timer_ticks]
9831 dunkaist 1696
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 1697
        ret
4700 mario79 1698
;--------------------------------------
1699
@@:
5794 serge 1700
; F.26.10 - get the time from kernel launch in nanoseconds
9571 Doczom 1701
        dec     ebx
5794 serge 1702
        jnz     @f
1703
 
1704
        call    get_clock_ns
9831 dunkaist 1705
        mov     [esp + SYSCALL_STACK.edx], edx
1706
        mov     [esp + SYSCALL_STACK.eax], eax
5794 serge 1707
        ret
1708
;--------------------------------------
1709
@@:
4700 mario79 1710
; F.26.11 - Find out whether low-level HD access is enabled
9571 Doczom 1711
        dec     ebx
4700 mario79 1712
        jnz     @f
1713
 
2288 clevermous 1714
        mov     eax, [lba_read_enabled]
9831 dunkaist 1715
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 1716
        ret
4700 mario79 1717
;--------------------------------------
1718
@@:
1719
; F.26.12 - Find out whether low-level PCI access is enabled
2288 clevermous 1720
        dec     ebx
4700 mario79 1721
        jnz     .error
1722
 
2288 clevermous 1723
        mov     eax, [pci_access_enabled]
9831 dunkaist 1724
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 1725
        ret
4700 mario79 1726
;--------------------------------------
1727
.error:
9831 dunkaist 1728
        or      [esp + SYSCALL_STACK.eax], dword -1
2288 clevermous 1729
        ret
4700 mario79 1730
;-----------------------------------------------------------------------------
2288 clevermous 1731
get_timer_ticks:
1732
        mov     eax, [timer_ticks]
1733
        ret
4700 mario79 1734
;-----------------------------------------------------------------------------
9911 Doczom 1735
iglobal
1736
midi_base dw 0
1737
endg
5851 pathoswith 1738
 
9911 Doczom 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:
2288 clevermous 1789
        push    edx
1790
        mov     dx, word [midisp]
1791
        in      al, dx
1792
        and     al, 0x80
1793
        pop     edx
1794
        ret
1795
 
9911 Doczom 1796
.is_output:
2288 clevermous 1797
        push    edx
1798
        mov     dx, word [midisp]
1799
        in      al, dx
1800
        and     al, 0x40
1801
        pop     edx
1802
        ret
1803
 
9911 Doczom 1804
.get_mpu_in:
2288 clevermous 1805
        push    edx
1806
        mov     dx, word [mididp]
1807
        in      al, dx
1808
        pop     edx
1809
        ret
1810
 
9911 Doczom 1811
.put_mpu_out:
2288 clevermous 1812
        push    edx
1813
        mov     dx, word [mididp]
1814
        out     dx, al
1815
        pop     edx
1816
        ret
1817
 
9911 Doczom 1818
;-----------------------------------------------------------------------------
2288 clevermous 1819
sys_end:
2430 mario79 1820
; restore default cursor before killing
1821
        pusha
1822
        mov     ecx, [current_slot]
9930 Doczom 1823
        mov     ecx, [ecx + APPDATA.window]
2435 mario79 1824
        call    restore_default_cursor_before_killing
2430 mario79 1825
        popa
1826
;--------------------------------------
3545 hidnplayr 1827
; kill all sockets this process owns
1828
        pusha
9612 Doczom 1829
        mov     edx, [current_slot]
9715 Doczom 1830
        mov     edx, [edx + APPDATA.tid]
6011 hidnplayr 1831
        call    socket_process_end
3545 hidnplayr 1832
        popa
1833
;--------------------------------------
2288 clevermous 1834
        mov     ecx, [current_slot]
9715 Doczom 1835
        mov     eax, [ecx + APPDATA.tls_base]
2288 clevermous 1836
        test    eax, eax
1837
        jz      @F
1838
 
1839
        stdcall user_free, eax
1840
@@:
1841
 
9612 Doczom 1842
        mov     eax, [current_slot]
9715 Doczom 1843
        mov     [eax + APPDATA.state], TSTATE_ZOMBIE
3534 clevermous 1844
        call    wakeup_osloop
2288 clevermous 1845
 
3597 Serge 1846
.waitterm:            ; wait here for termination
1847
        call    change_task
1848
        jmp     .waitterm
2435 mario79 1849
;------------------------------------------------------------------------------
2452 mario79 1850
align 4
9930 Doczom 1851
; ecx - ptr WDATA
2435 mario79 1852
restore_default_cursor_before_killing:
2468 mario79 1853
        pushfd
1854
        cli
2435 mario79 1855
        mov     eax, [def_cursor]
9930 Doczom 1856
        mov     [ecx + WDATA.cursor], eax
2288 clevermous 1857
 
2435 mario79 1858
        movzx   eax, word [MOUSE_Y]
1859
        movzx   ebx, word [MOUSE_X]
2446 mario79 1860
        mov     eax, [d_width_calc_area + eax*4]
1861
 
5351 serge 1862
        add     eax, [_display.win_map]
9715 Doczom 1863
        movzx   edx, byte [ebx + eax]
9930 Doczom 1864
        shl     edx, BSF sizeof.WDATA
1865
        mov     esi, [window_data + edx + WDATA.cursor]
2452 mario79 1866
 
1867
        cmp     esi, [current_cursor]
1868
        je      @f
1869
 
9941 Doczom 1870
        cmp     [_display.select_cursor], 0
1871
        jz      @f
1872
 
1873
        stdcall [_display.select_cursor], esi
2435 mario79 1874
        mov     [current_cursor], esi
2452 mario79 1875
@@:
2450 mario79 1876
        mov     [redrawmouse_unconditional], 1
3534 clevermous 1877
        call    wakeup_osloop
2468 mario79 1878
        popfd
2435 mario79 1879
        ret
1880
;------------------------------------------------------------------------------
2288 clevermous 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
9268 Doczom 1895
        dd      undefined_syscall       ; 12 = get last pressed key. function removed. sysfn_lastkey
2288 clevermous 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
2648 mario79 1907
        dd      sysfn_min_windows       ; 23 = minimize all windows
2654 mario79 1908
        dd      sysfn_set_screen_sizes  ; 24 = set screen sizes for Vesa
5836 GerdtR 1909
 
1910
        dd      sysfn_zmodif            ; 25 = get/set window z modifier  ;Fantomer
2288 clevermous 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
7122 dunkaist 1923
        cmp     ecx, SYSTEM_SHUTDOWN
9930 Doczom 1924
        jl      .exit_for_anyone
7122 dunkaist 1925
        cmp     ecx, SYSTEM_RESTART
9930 Doczom 1926
        jg      .exit_for_anyone
7132 dunkaist 1927
        mov     [BOOT.shutdown_type], cl
2288 clevermous 1928
 
8866 rgimad 1929
        mov     eax, [thread_count]
2288 clevermous 1930
        mov     [SYS_SHUTDOWN], al
1931
        mov     [shutdown_processes], eax
3534 clevermous 1932
        call    wakeup_osloop
9831 dunkaist 1933
        and     dword [esp + SYSCALL_STACK.eax], 0
9930 Doczom 1934
.exit_for_anyone:
2288 clevermous 1935
        ret
1936
  uglobal
1937
   shutdown_processes:
1938
                       dd 0x0
1939
  endg
1940
;------------------------------------------------------------------------------
8270 dunkaist 1941
; in: eax -- APPDATA ptr
1942
; out: Z/z -- is/not kernel thread
1943
is_kernel_thread:
9715 Doczom 1944
        mov     eax, [eax + APPDATA.process]
1945
        cmp     eax, [SLOT_BASE + 2*sizeof.APPDATA + APPDATA.process]       ; OS
8270 dunkaist 1946
        ret
1947
;------------------------------------------------------------------------------
2288 clevermous 1948
sysfn_terminate:        ; 18.2 = TERMINATE
2472 mario79 1949
        push    ecx
2288 clevermous 1950
        cmp     ecx, 2
9910 Doczom 1951
        jb      .noprocessterminate
8866 rgimad 1952
        mov     edx, [thread_count]
2288 clevermous 1953
        cmp     ecx, edx
9910 Doczom 1954
        ja      .noprocessterminate
8866 rgimad 1955
        mov     eax, [thread_count]
9709 Doczom 1956
        shl     ecx, BSF sizeof.APPDATA
1957
        add     ecx, SLOT_BASE
9911 Doczom 1958
 
9709 Doczom 1959
        mov     edx, [ecx + APPDATA.tid]
1960
        cmp     byte [ecx + APPDATA.state], TSTATE_FREE
9910 Doczom 1961
        jz      .noprocessterminate
8270 dunkaist 1962
        push    eax
9709 Doczom 1963
        mov     eax, ecx
8270 dunkaist 1964
        call    is_kernel_thread
1965
        pop     eax
9910 Doczom 1966
        jz      .noprocessterminate
3296 clevermous 1967
        push    ecx edx
9709 Doczom 1968
        mov     edx, ecx
3296 clevermous 1969
        call    request_terminate
1970
        pop     edx ecx
1971
        test    eax, eax
9910 Doczom 1972
        jz      .noprocessterminate
2430 mario79 1973
;--------------------------------------
3545 hidnplayr 1974
; terminate all network sockets it used
1975
        pusha
9709 Doczom 1976
        mov     eax, edx     ;TODO: check function
6011 hidnplayr 1977
        call    socket_process_end
3545 hidnplayr 1978
        popa
1979
;--------------------------------------
2430 mario79 1980
; restore default cursor before killing
1981
        pusha
1982
        mov     ecx, [esp+32]
9930 Doczom 1983
        shl     ecx, BSF sizeof.WDATA
1984
        add     ecx, window_data
2452 mario79 1985
        mov     eax, [def_cursor]
9930 Doczom 1986
        cmp     [ecx + WDATA.cursor], eax
2452 mario79 1987
        je      @f
2435 mario79 1988
        call    restore_default_cursor_before_killing
2452 mario79 1989
@@:
2430 mario79 1990
        popa
1991
;--------------------------------------
2288 clevermous 1992
     ;call MEM_Heap_Lock      ;guarantee that process isn't working with heap
9709 Doczom 1993
        mov     [ecx + APPDATA.state], TSTATE_ZOMBIE; clear possible i40's
3534 clevermous 1994
        call    wakeup_osloop
2288 clevermous 1995
     ;call MEM_Heap_UnLock
1996
 
3534 clevermous 1997
        cmp     edx, [application_table_owner]; clear app table stat
9910 Doczom 1998
        jne     .noatsc
3534 clevermous 1999
        call    unlock_application_table
9910 Doczom 2000
.noatsc:
2001
.noprocessterminate:
2472 mario79 2002
        add     esp, 4
2288 clevermous 2003
        ret
2004
;------------------------------------------------------------------------------
2005
sysfn_terminate2:
3534 clevermous 2006
        call    lock_application_table
2288 clevermous 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
3534 clevermous 2014
        call    unlock_application_table
2288 clevermous 2015
        sti
9831 dunkaist 2016
        and     dword [esp + SYSCALL_STACK.eax], 0
2288 clevermous 2017
        ret
2018
.not_found:
3534 clevermous 2019
        call    unlock_application_table
9831 dunkaist 2020
        or      dword [esp + SYSCALL_STACK.eax], -1
2288 clevermous 2021
        ret
2022
;------------------------------------------------------------------------------
2023
sysfn_deactivate:         ; 18.1 = DEACTIVATE WINDOW
2024
        cmp     ecx, 2
2025
        jb      .nowindowdeactivate
8866 rgimad 2026
        cmp     ecx, [thread_count]
2288 clevermous 2027
        ja      .nowindowdeactivate
2408 Serge 2028
 
2288 clevermous 2029
        movzx   esi, word [WIN_STACK + ecx*2]
2030
        cmp     esi, 1
2031
        je      .nowindowdeactivate ; already deactive
2032
 
2033
        mov     edi, ecx
9926 Doczom 2034
        shl     edi, BSF sizeof.WDATA
2288 clevermous 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
6800 pathoswith 2039
        call    syscall_display_settings.calculateScreen
2040
        call    syscall_display_settings.redrawScreen
2288 clevermous 2041
.nowindowdeactivate:
2042
        ret
3455 mario79 2043
;------------------------------------------------------------------------------
2288 clevermous 2044
sysfn_activate:         ; 18.3 = ACTIVATE WINDOW
5675 leency 2045
        cmp     ecx, 2
2046
        jb      .nowindowactivate
8866 rgimad 2047
        cmp     ecx, [thread_count]
2288 clevermous 2048
        ja      .nowindowactivate
3455 mario79 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
;-------------------------------------
2288 clevermous 2061
        mov     [window_minimize], 2; restore window if minimized
3534 clevermous 2062
        call    wakeup_osloop
2288 clevermous 2063
 
2064
        movzx   esi, word [WIN_STACK + ecx*2]
8866 rgimad 2065
        cmp     esi, [thread_count]
2288 clevermous 2066
        je      .nowindowactivate; already active
2067
 
2068
        mov     edi, ecx
9715 Doczom 2069
        shl     edi, BSF sizeof.WDATA
2288 clevermous 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
;------------------------------------------------------------------------------
5865 GerdtR 2077
align 4
5836 GerdtR 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
8869 rgimad 2089
        mov     edx, [current_slot_idx]
5836 GerdtR 2090
     @@:
8866 rgimad 2091
        cmp     edx, [thread_count]
5865 GerdtR 2092
        ja      .fail
5836 GerdtR 2093
        cmp     edx, 1
5865 GerdtR 2094
        je      .fail
5836 GerdtR 2095
 
5865 GerdtR 2096
        mov     eax, edx
9715 Doczom 2097
        shl     edx, BSF sizeof.WDATA
9949 Doczom 2098
        add     edx, window_data
5836 GerdtR 2099
 
9949 Doczom 2100
        test    [edx + WDATA.fl_wstate], WSTATE_USED
9930 Doczom 2101
        jz      .fail
5836 GerdtR 2102
 
2103
        cmp     ecx, 1
2104
        jnz     .set_zmod
2105
 
9949 Doczom 2106
        movzx   eax, [edx + WDATA.z_modif]
5865 GerdtR 2107
        jmp     .exit
5836 GerdtR 2108
 
2109
.set_zmod:
2110
        cmp     ecx, 2
5865 GerdtR 2111
        jnz     .fail
5836 GerdtR 2112
 
5865 GerdtR 2113
        mov     ebx, esi
2114
        mov     esi, eax
5836 GerdtR 2115
 
5865 GerdtR 2116
        cmp     bl, ZPOS_ALWAYS_TOP
2117
        jg      .fail
5836 GerdtR 2118
 
9949 Doczom 2119
        mov     [edx + WDATA.z_modif], bl
5836 GerdtR 2120
 
9949 Doczom 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]
5836 GerdtR 2125
        add     ecx, eax
2126
        add     edx, ebx
5865 GerdtR 2127
        call    window._.set_screen
5836 GerdtR 2128
        call    window._.set_top_wnd
5865 GerdtR 2129
        call    window._.redraw_top_wnd
5836 GerdtR 2130
 
9715 Doczom 2131
        shl     esi, BSF sizeof.WDATA
9930 Doczom 2132
        mov     [esi + window_data + WDATA.fl_redraw], WSTATE_REDRAW
5865 GerdtR 2133
 
2134
 
5836 GerdtR 2135
        mov     eax, 1
5865 GerdtR 2136
        jmp     .exit
2137
.fail:
2138
        xor     eax, eax
5836 GerdtR 2139
.exit:
9831 dunkaist 2140
        mov     [esp + SYSCALL_STACK.eax], eax
5836 GerdtR 2141
        ret
2142
 
2143
;------------------------------------------------------------------------------
2288 clevermous 2144
sysfn_getidletime:              ; 18.4 = GET IDLETIME
9932 Doczom 2145
        mov     eax, [SLOT_BASE + sizeof.APPDATA + APPDATA.cpu_usage]
9831 dunkaist 2146
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2147
        ret
2148
;------------------------------------------------------------------------------
2149
sysfn_getcpuclock:              ; 18.5 = GET TSC/SEC
3481 Serge 2150
        mov     eax, dword [cpu_freq]
9831 dunkaist 2151
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2152
        ret
2153
;------------------------------------------------------------------------------
3481 Serge 2154
get_cpu_freq:
2155
        mov     eax, dword [cpu_freq]
2156
        mov     edx, dword [cpu_freq+4]
2157
        ret
9268 Doczom 2158
;  SAVE ramdisk to /hd/1/kolibri.img
2288 clevermous 2159
;!!!!!!!!!!!!!!!!!!!!!!!!
2160
   include 'blkdev/rdsave.inc'
2161
;!!!!!!!!!!!!!!!!!!!!!!!!
2162
;------------------------------------------------------------------------------
2163
align 4
2164
sysfn_getactive:        ; 18.7 = get active window
8866 rgimad 2165
        mov     eax, [thread_count]
2288 clevermous 2166
        movzx   eax, word [WIN_POS + eax*2]
9831 dunkaist 2167
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2168
        ret
2169
;------------------------------------------------------------------------------
2170
sysfn_sound_flag:       ; 18.8 = get/set sound_flag
2171
;     cmp  ecx,1
2172
        dec     ecx
9911 Doczom 2173
        jnz     .set_flag
2288 clevermous 2174
        movzx   eax, byte [sound_flag]; get sound_flag
9831 dunkaist 2175
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2176
        ret
9911 Doczom 2177
.set_flag:
2288 clevermous 2178
;     cmp  ecx,2
2179
        dec     ecx
9911 Doczom 2180
        jnz     .err
2288 clevermous 2181
        xor     byte [sound_flag], 1
9911 Doczom 2182
.err:
2288 clevermous 2183
        ret
2184
;------------------------------------------------------------------------------
2185
sysfn_minimize:         ; 18.10 = minimize window
2186
        mov     [window_minimize], 1
3534 clevermous 2187
        call    wakeup_osloop
2288 clevermous 2188
        ret
2189
;------------------------------------------------------------------------------
2190
align 4
2191
sysfn_getdiskinfo:      ; 18.11 = get disk info table
2192
        dec     ecx
4641 mario79 2193
        jnz     .exit
2194
.small_table:
8598 rgimad 2195
        stdcall is_region_userspace, edx, DRIVE_DATA_SIZE
9045 dunkaist 2196
        jnz     .exit
2288 clevermous 2197
        mov     edi, edx
2198
        mov     esi, DRIVE_DATA
4641 mario79 2199
        mov     ecx, DRIVE_DATA_SIZE ;10
2288 clevermous 2200
        cld
4641 mario79 2201
        rep movsb
2202
.exit:
2288 clevermous 2203
        ret
2204
;------------------------------------------------------------------------------
2205
sysfn_getversion:       ; 18.13 = get kernel ID and version
8247 rgimad 2206
        ; if given memory address belongs to kernel then error
9917 Doczom 2207
        stdcall is_region_userspace, ecx, version_inf.size
9045 dunkaist 2208
        jnz     .addr_error
8247 rgimad 2209
 
2288 clevermous 2210
        mov     edi, ecx
2211
        mov     esi, version_inf
9917 Doczom 2212
        mov     ecx, version_inf.size
2288 clevermous 2213
        rep movsb
2214
        ret
8247 rgimad 2215
.addr_error:    ; if given memory address is illegal
9831 dunkaist 2216
        mov     dword [esp + SYSCALL_STACK.eax], -1
9947 ace_dent 2217
        ret
2288 clevermous 2218
;------------------------------------------------------------------------------
2219
sysfn_waitretrace:     ; 18.14 = sys wait retrace
9911 Doczom 2220
        ;wait retrace functions
2288 clevermous 2221
        mov     edx, 0x3da
9911 Doczom 2222
.loop:
2288 clevermous 2223
        in      al, dx
2224
        test    al, 1000b
9911 Doczom 2225
        jz      .loop
9831 dunkaist 2226
        and     [esp + SYSCALL_STACK.eax], dword 0
2288 clevermous 2227
        ret
2228
;------------------------------------------------------------------------------
2229
align 4
2230
sysfn_centermouse:      ; 18.15 = mouse centered
5350 serge 2231
        mov     eax, [_display.width]
2288 clevermous 2232
        shr     eax, 1
2233
        mov     [MOUSE_X], ax
5350 serge 2234
        mov     eax, [_display.height]
2288 clevermous 2235
        shr     eax, 1
2236
        mov     [MOUSE_Y], ax
3534 clevermous 2237
        call    wakeup_osloop
2288 clevermous 2238
        xor     eax, eax
9831 dunkaist 2239
        and     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2240
        ret
2241
;------------------------------------------------------------------------------
5851 pathoswith 2242
sysfn_mouse_acceleration:       ; 18.19 = set/get mouse features
2243
        cmp     ecx, 8
2244
        jnc     @f
9715 Doczom 2245
        jmp     dword [.table + ecx*4]
5851 pathoswith 2246
.get_mouse_acceleration:
2288 clevermous 2247
        xor     eax, eax
2248
        mov     ax, [mouse_speed_factor]
9831 dunkaist 2249
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2250
        ret
5851 pathoswith 2251
.set_mouse_acceleration:
2288 clevermous 2252
        mov     [mouse_speed_factor], dx
2253
        ret
5851 pathoswith 2254
.get_mouse_delay:
5853 pathoswith 2255
        xor     eax, eax
2256
        mov     al, [mouse_delay]
9831 dunkaist 2257
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2258
        ret
5851 pathoswith 2259
.set_mouse_delay:
5853 pathoswith 2260
        mov     [mouse_delay], dl
5851 pathoswith 2261
@@:
2288 clevermous 2262
        ret
5851 pathoswith 2263
.set_pointer_position:
5350 serge 2264
        cmp     dx, word[_display.height]
5851 pathoswith 2265
        jae     @b
2288 clevermous 2266
        rol     edx, 16
5350 serge 2267
        cmp     dx, word[_display.width]
5851 pathoswith 2268
        jae     @b
2288 clevermous 2269
        mov     [MOUSE_X], edx
4381 hidnplayr 2270
        mov     [mouse_active], 1
5851 pathoswith 2271
        jmp     wakeup_osloop
2272
.set_mouse_button:
2273
        mov     [BTN_DOWN], edx
2288 clevermous 2274
        mov     [mouse_active], 1
5851 pathoswith 2275
        jmp     wakeup_osloop
2276
.get_doubleclick_delay:
2277
        xor     eax, eax
2278
        mov     al, [mouse_doubleclick_delay]
9831 dunkaist 2279
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2280
        ret
5851 pathoswith 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
2288 clevermous 2294
;------------------------------------------------------------------------------
2295
sysfn_getfreemem:
2296
        mov     eax, [pg_data.pages_free]
2297
        shl     eax, 2
9831 dunkaist 2298
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2299
        ret
2648 mario79 2300
;------------------------------------------------------------------------------
2288 clevermous 2301
sysfn_getallmem:
2302
        mov     eax, [MEM_AMOUNT]
2303
        shr     eax, 10
9831 dunkaist 2304
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2305
        ret
2648 mario79 2306
;------------------------------------------------------------------------------
2288 clevermous 2307
sysfn_pid_to_slot:
2308
        mov     eax, ecx
2309
        call    pid_to_slot
9831 dunkaist 2310
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2311
        ret
2648 mario79 2312
;------------------------------------------------------------------------------
2288 clevermous 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
9828 Doczom 2322
        cmp     eax, max_processes    ; varify maximal slot number
2288 clevermous 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
9831 dunkaist 2335
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2336
        ret
2337
.error:
2338
        popad
2339
        xor     eax, eax
2340
        dec     eax
9831 dunkaist 2341
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2342
        ret
2648 mario79 2343
;------------------------------------------------------------------------------
2344
sysfn_min_windows:
2345
        call    minimize_all_window
9831 dunkaist 2346
        mov     [esp + SYSCALL_STACK.eax], eax
2648 mario79 2347
        call    change_task
2348
        ret
2349
;------------------------------------------------------------------------------
2654 mario79 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
5351 serge 2366
        mov     ecx, [_display.lfb_pitch]
2654 mario79 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
;------------------------------------------------------------------------------
2288 clevermous 2381
uglobal
2382
screen_workarea RECT
2654 mario79 2383
display_width_standard dd 0
2384
display_height_standard dd 0
2385
do_not_touch_winmap db 0
2288 clevermous 2386
window_minimize db 0
2387
sound_flag      db 0
2654 mario79 2388
 
2288 clevermous 2389
endg
2390
 
2391
iglobal
2392
version_inf:
3454 mario79 2393
        db 0,7,7,0  ; version 0.7.7.0
2394
        db 0
2395
.rev    dd __REV__
9917 Doczom 2396
.size = $ - version_inf
2288 clevermous 2397
endg
2515 mario79 2398
;------------------------------------------------------------------------------
2399
align 4
2288 clevermous 2400
sys_cachetodiskette:
2401
        cmp     ebx, 1
4273 clevermous 2402
        jb      .no_floppy_save
2288 clevermous 2403
        cmp     ebx, 2
4273 clevermous 2404
        ja      .no_floppy_save
2288 clevermous 2405
        call    save_image
9831 dunkaist 2406
        mov     [esp + SYSCALL_STACK.eax], eax
4273 clevermous 2407
        ret
2408
.no_floppy_save:
9831 dunkaist 2409
        mov     [esp + SYSCALL_STACK.eax], dword 1
2288 clevermous 2410
        ret
2515 mario79 2411
;------------------------------------------------------------------------------
2412
align 4
2288 clevermous 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
;
8246 rgimad 2425
        ; if given memory address belongs to kernel then error
8329 rgimad 2426
        stdcall is_region_userspace, ebx, 0x4C
9045 dunkaist 2427
        jnz     .addr_error
2288 clevermous 2428
 
2429
        cmp     ecx, -1 ; who am I ?
2430
        jne     .no_who_am_i
8869 rgimad 2431
        mov     ecx, [current_slot_idx]
2288 clevermous 2432
  .no_who_am_i:
2433
        cmp     ecx, max_processes
2434
        ja      .nofillbuf
2435
 
2436
; +4: word: position of the window of thread in the window stack
2437
        mov     ax, [WIN_STACK + ecx * 2]
2438
        mov     [ebx+4], ax
2439
; +6: word: number of the thread slot, which window has in the window stack
2440
;           position ecx (has no relation to the specific thread)
2441
        mov     ax, [WIN_POS + ecx * 2]
2442
        mov     [ebx+6], ax
2443
 
9715 Doczom 2444
        shl     ecx, BSF sizeof.APPDATA
2288 clevermous 2445
 
2446
; +0: dword: memory usage
9932 Doczom 2447
        mov     eax, [SLOT_BASE + ecx + APPDATA.cpu_usage]
2288 clevermous 2448
        mov     [ebx], eax
2449
; +10: 11 bytes: name of the process
2450
        push    ecx
9715 Doczom 2451
        lea     eax, [SLOT_BASE + ecx + APPDATA.app_name]
2288 clevermous 2452
        add     ebx, 10
2453
        mov     ecx, 11
2454
        call    memmove
2455
        pop     ecx
2456
 
2457
; +22: address of the process in memory
2458
; +26: size of used memory - 1
2459
        push    edi
2460
        lea     edi, [ebx+12]
2461
        xor     eax, eax
2462
        mov     edx, 0x100000*16
9932 Doczom 2463
        cmp     ecx, 1 shl BSF sizeof.APPDATA
2288 clevermous 2464
        je      .os_mem
9932 Doczom 2465
        mov     edx, [SLOT_BASE + ecx + APPDATA.process]
9715 Doczom 2466
        mov     edx, [edx + PROC.mem_used]
2288 clevermous 2467
        mov     eax, std_application_base_address
2468
.os_mem:
2469
        stosd
2470
        lea     eax, [edx-1]
2471
        stosd
2472
 
9941 Doczom 2473
        mov     edx, [SLOT_BASE + ecx + APPDATA.window]
9932 Doczom 2474
 
2288 clevermous 2475
; +30: PID/TID
9932 Doczom 2476
        mov     eax, [SLOT_BASE + ecx + APPDATA.tid]
2288 clevermous 2477
        stosd
2478
 
2479
    ; window position and size
2480
        push    esi
9941 Doczom 2481
        lea     esi, [edx + WDATA.box]
2288 clevermous 2482
        movsd
2483
        movsd
2484
        movsd
2485
        movsd
2486
 
2487
    ; Process state (+50)
9932 Doczom 2488
        movzx   eax, byte [SLOT_BASE + ecx + APPDATA.state]
2288 clevermous 2489
        stosd
2490
 
2491
    ; Window client area box
9941 Doczom 2492
        lea     esi, [edx + WDATA.clientbox]
2288 clevermous 2493
        movsd
2494
        movsd
2495
        movsd
2496
        movsd
2497
 
2498
    ; Window state
9941 Doczom 2499
        mov     al, [edx + WDATA.fl_wstate]
2288 clevermous 2500
        stosb
2501
 
2502
    ; Event mask (+71)
9932 Doczom 2503
        mov     eax, dword [SLOT_BASE + ecx + APPDATA.event_mask]
2288 clevermous 2504
        stosd
4286 Serge 2505
 
4262 0CodErr 2506
    ; Keyboard mode (+75)
9932 Doczom 2507
        mov     al, byte [SLOT_BASE + ecx + APPDATA.keyboard_mode]
4286 Serge 2508
        stosb
2288 clevermous 2509
 
2510
        pop     esi
2511
        pop     edi
2512
 
2513
.nofillbuf:
2514
    ; return number of processes
2515
 
8866 rgimad 2516
        mov     eax, [thread_count]
9831 dunkaist 2517
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2518
        ret
2519
 
8246 rgimad 2520
.addr_error:    ; if given memory address is illegal
9831 dunkaist 2521
        mov     dword [esp + SYSCALL_STACK.eax], -1
9947 ace_dent 2522
        ret
8246 rgimad 2523
 
2288 clevermous 2524
 
9612 Doczom 2525
; redraw status
2288 clevermous 2526
align 4
2527
sys_redrawstat:
2528
        cmp     ebx, 1
9910 Doczom 2529
        jne     .no_widgets_away
2288 clevermous 2530
        ; buttons away
8869 rgimad 2531
        mov     ecx, [current_slot_idx]
9910 Doczom 2532
.sys_newba2:
2288 clevermous 2533
        mov     edi, [BTN_ADDR]
2534
        cmp     [edi], dword 0  ; empty button list ?
9910 Doczom 2535
        je      .end_of_buttons_away
2288 clevermous 2536
        movzx   ebx, word [edi]
2537
        inc     ebx
2538
        mov     eax, edi
9910 Doczom 2539
.sys_newba:
2288 clevermous 2540
        dec     ebx
9910 Doczom 2541
        jz      .end_of_buttons_away
2288 clevermous 2542
 
2543
        add     eax, 0x10
2544
        cmp     cx, [eax]
9910 Doczom 2545
        jnz     .sys_newba
2288 clevermous 2546
 
2547
        push    eax ebx ecx
2548
        mov     ecx, ebx
2549
        inc     ecx
2550
        shl     ecx, 4
2551
        mov     ebx, eax
2552
        add     eax, 0x10
2553
        call    memmove
2554
        dec     dword [edi]
2555
        pop     ecx ebx eax
2556
 
9910 Doczom 2557
        jmp     .sys_newba2
2288 clevermous 2558
 
9910 Doczom 2559
.end_of_buttons_away:
2288 clevermous 2560
        ret
2561
 
9910 Doczom 2562
.no_widgets_away:
2288 clevermous 2563
 
2564
        cmp     ebx, 2
9910 Doczom 2565
        jnz     .srl1
2288 clevermous 2566
 
9930 Doczom 2567
        mov     edx, [current_slot]      ; return whole screen draw area for this app
2568
        mov     edx, [edx + APPDATA.window]
2569
        mov     [edx + WDATA.draw_data.left], 0
2570
        mov     [edx + WDATA.draw_data.top], 0
5350 serge 2571
        mov     eax, [_display.width]
2572
        dec     eax
9930 Doczom 2573
        mov     [edx + WDATA.draw_data.right], eax
5350 serge 2574
        mov     eax, [_display.height]
2575
        dec     eax
9930 Doczom 2576
        mov     [edx + WDATA.draw_data.bottom], eax
2288 clevermous 2577
 
9910 Doczom 2578
.srl1:
2288 clevermous 2579
        ret
2580
 
2581
;ok - 100% work
2582
;nt - not tested
2583
;---------------------------------------------------------------------------------------------
2584
;eax
2585
;0 - task switch counter. Ret switch counter in eax. Block. ok.
2586
;1 - change task. Ret nothing. Block. ok.
2587
;2 - performance control
2588
; ebx
2589
; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
2590
; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
2591
; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
2592
; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
2593
; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
2594
;eax
2595
;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
2596
;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
2597
;---------------------------------------------------------------------------------------------
2598
iglobal
2599
align 4
2600
sheduler:
2601
        dd      sys_sheduler.00
2602
        dd      change_task
2603
        dd      sys_sheduler.02
2604
        dd      sys_sheduler.03
2605
        dd      sys_sheduler.04
2606
endg
2607
sys_sheduler:
2608
;rewritten by   29.12.2009
9715 Doczom 2609
        jmp     dword [sheduler + ebx*4]
2288 clevermous 2610
;.shed_counter:
2611
.00:
2612
        mov     eax, [context_counter]
9831 dunkaist 2613
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2614
        ret
2615
 
2616
.02:
2617
;.perf_control:
2618
        inc     ebx                     ;before ebx=2, ebx=3
2619
        cmp     ebx, ecx                ;if ecx=3, ebx=3
2620
        jz      cache_disable
2621
 
2622
        dec     ebx                     ;ebx=2
2623
        cmp     ebx, ecx                ;
2624
        jz      cache_enable            ;if ecx=2 and ebx=2
2625
 
2626
        dec     ebx                     ;ebx=1
2627
        cmp     ebx, ecx
2628
        jz      is_cache_enabled        ;if ecx=1 and ebx=1
2629
 
2630
        dec     ebx
2631
        test    ebx, ecx                ;ebx=0 and ecx=0
2632
        jz      modify_pce              ;if ecx=0
2633
 
2634
        ret
2635
 
2636
.03:
2637
;.rdmsr_instr:
2638
;now counter in ecx
2639
;(edx:eax) esi:edi => edx:esi
2640
        mov     eax, esi
2641
        mov     ecx, edx
2642
        rdmsr
9831 dunkaist 2643
        mov     [esp + SYSCALL_STACK.eax], eax
2644
        mov     [esp + SYSCALL_STACK.ebx], edx           ;ret in ebx?
2288 clevermous 2645
        ret
2646
 
2647
.04:
2648
;.wrmsr_instr:
2649
;now counter in ecx
2650
;(edx:eax) esi:edi => edx:esi
2651
        ; Fast Call MSR can't be destroy
3539 clevermous 2652
        ; Но MSR_AMD_EFER можно изменять, т.к. в этом регистре лиш
2653
        ; включаются/выключаются расширенные возможности
2288 clevermous 2654
        cmp     edx, MSR_SYSENTER_CS
2655
        je      @f
2656
        cmp     edx, MSR_SYSENTER_ESP
2657
        je      @f
2658
        cmp     edx, MSR_SYSENTER_EIP
2659
        je      @f
2660
        cmp     edx, MSR_AMD_STAR
2661
        je      @f
2662
 
2663
        mov     eax, esi
2664
        mov     ecx, edx
2665
        wrmsr
2666
        ; mov   [esp + 32], eax
2667
        ; mov   [esp + 20], edx ;ret in ebx?
2668
@@:
2669
        ret
2670
 
2671
cache_disable:
2672
        mov     eax, cr0
9831 dunkaist 2673
        or      eax, 01100000_00000000_00000000_00000000b
2288 clevermous 2674
        mov     cr0, eax
2675
        wbinvd  ;set MESI
2676
        ret
2677
 
2678
cache_enable:
2679
        mov     eax, cr0
9831 dunkaist 2680
        and     eax, 10011111_11111111_11111111_11111111b
2288 clevermous 2681
        mov     cr0, eax
2682
        ret
2683
 
2684
is_cache_enabled:
2685
        mov     eax, cr0
9831 dunkaist 2686
        and     eax, 01100000_00000000_00000000_00000000b
9941 Doczom 2687
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2688
        ret
2689
 
2690
modify_pce:
2691
        mov     eax, cr4
2692
;       mov ebx,0
2693
;       or  bx,100000000b ;pce
2694
;       xor eax,ebx ;invert pce
2695
        bts     eax, 8;pce=cr4[8]
2696
        mov     cr4, eax
9831 dunkaist 2697
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 2698
        ret
2699
;---------------------------------------------------------------------------------------------
2700
 
2701
 
2702
iglobal
2703
  cpustring db 'CPU',0
2704
endg
2705
 
2706
uglobal
2707
background_defined    db    0    ; diamond, 11.04.2006
2708
endg
2513 mario79 2709
;-----------------------------------------------------------------------------
2288 clevermous 2710
align 4
2711
checkmisc:
2712
        cmp     [ctrl_alt_del], 1
9910 Doczom 2713
        jne     .nocpustart
2288 clevermous 2714
 
2715
        mov     ebp, cpustring
2716
        call    fs_execute_from_sysdir
2717
 
2718
        mov     [ctrl_alt_del], 0
2513 mario79 2719
;--------------------------------------
2720
align 4
9910 Doczom 2721
.nocpustart:
2288 clevermous 2722
        cmp     [mouse_active], 1
9910 Doczom 2723
        jne     .mouse_not_active
2288 clevermous 2724
        mov     [mouse_active], 0
2411 Serge 2725
 
2288 clevermous 2726
        xor     edi, edi
9709 Doczom 2727
        mov     ebx, window_data
2411 Serge 2728
 
8866 rgimad 2729
        mov     ecx, [thread_count]
2408 Serge 2730
        movzx   eax, word [WIN_POS + ecx*2]     ; active window
9925 Doczom 2731
        shl     eax, BSF sizeof.APPDATA
2414 Serge 2732
        push    eax
2408 Serge 2733
 
2414 Serge 2734
        movzx   eax, word [MOUSE_X]
2735
        movzx   edx, word [MOUSE_Y]
2513 mario79 2736
;--------------------------------------
2408 Serge 2737
align 4
2738
.set_mouse_event:
8093 dunkaist 2739
        add     edi, sizeof.APPDATA
9709 Doczom 2740
        add     ebx, sizeof.WDATA
9917 Doczom 2741
        test    [SLOT_BASE + edi + APPDATA.event_mask], EVM_MOUSE_FILTER
2414 Serge 2742
        jz      .pos_filter
2743
 
2744
        cmp     edi, [esp]                      ; skip if filtration active
2745
        jne     .skip
2513 mario79 2746
;--------------------------------------
2747
align 4
2414 Serge 2748
.pos_filter:
9917 Doczom 2749
        test    [SLOT_BASE + edi + APPDATA.event_mask], EVM_CURSOR_FILTER
2411 Serge 2750
        jz      .set
2288 clevermous 2751
 
9709 Doczom 2752
        mov     esi, [ebx + WDATA.box.left]
2414 Serge 2753
        cmp     eax, esi
2754
        jb      .skip
9709 Doczom 2755
        add     esi, [ebx + WDATA.box.width]
2414 Serge 2756
        cmp     eax, esi
2757
        ja      .skip
2758
 
9709 Doczom 2759
        mov     esi, [ebx + WDATA.box.top]
2414 Serge 2760
        cmp     edx, esi
2761
        jb      .skip
9709 Doczom 2762
        add     esi, [ebx + WDATA.box.height]
2414 Serge 2763
        cmp     edx, esi
2764
        ja      .skip
2513 mario79 2765
;--------------------------------------
2766
align 4
2411 Serge 2767
.set:
9715 Doczom 2768
        or      [SLOT_BASE + edi + APPDATA.occurred_events], EVENT_MOUSE
2513 mario79 2769
;--------------------------------------
2770
align 4
2411 Serge 2771
.skip:
2408 Serge 2772
        loop    .set_mouse_event
2773
 
2414 Serge 2774
        pop     eax
2513 mario79 2775
;--------------------------------------
2776
align 4
9910 Doczom 2777
.mouse_not_active:
6585 pathoswith 2778
        cmp     [REDRAW_BACKGROUND], 0  ; background update ?
2288 clevermous 2779
        jz      nobackgr
2524 mario79 2780
 
2288 clevermous 2781
        cmp     [background_defined], 0
2782
        jz      nobackgr
2513 mario79 2783
;--------------------------------------
2784
align 4
3536 clevermous 2785
backgr:
9930 Doczom 2786
        mov     eax, [background_window + WDATA.draw_data.left]
2537 mario79 2787
        shl     eax, 16
9930 Doczom 2788
        add     eax, [background_window + WDATA.draw_data.right]
2537 mario79 2789
        mov     [BG_Rect_X_left_right], eax ; [left]*65536 + [right]
2790
 
9930 Doczom 2791
        mov     eax, [background_window + WDATA.draw_data.top]
2537 mario79 2792
        shl     eax, 16
9930 Doczom 2793
        add     eax, [background_window + WDATA.draw_data.bottom]
2537 mario79 2794
        mov     [BG_Rect_Y_top_bottom], eax ; [top]*65536 + [bottom]
2795
 
2288 clevermous 2796
        call    drawbackground
2580 mario79 2797
;        DEBUGF  1, "K : drawbackground\n"
2620 mario79 2798
;        DEBUGF  1, "K : backg x %x\n",[BG_Rect_X_left_right]
2799
;        DEBUGF  1, "K : backg y %x\n",[BG_Rect_Y_top_bottom]
2513 mario79 2800
;--------- set event 5 start ----------
2801
        push    ecx edi
9941 Doczom 2802
        mov     edi, window_data
8866 rgimad 2803
        mov     ecx, [thread_count]
2513 mario79 2804
;--------------------------------------
2805
align 4
2806
set_bgr_event:
9930 Doczom 2807
        add     edi, sizeof.WDATA
3536 clevermous 2808
        mov     eax, [BG_Rect_X_left_right]
2809
        mov     edx, [BG_Rect_Y_top_bottom]
9941 Doczom 2810
        cmp     [edi + WDATA.draw_bgr_x], 0
3536 clevermous 2811
        jz      .set
2812
.join:
9941 Doczom 2813
        cmp     word [edi + WDATA.draw_bgr_x], ax
3611 clevermous 2814
        jae     @f
9941 Doczom 2815
        mov     word [edi + WDATA.draw_bgr_x], ax
3611 clevermous 2816
@@:
2817
        shr     eax, 16
9941 Doczom 2818
        cmp     word [edi + WDATA.draw_bgr_x + 2], ax
3536 clevermous 2819
        jbe     @f
9941 Doczom 2820
        mov     word [edi + WDATA.draw_bgr_x + 2], ax
3536 clevermous 2821
@@:
9941 Doczom 2822
        cmp     word [edi + WDATA.draw_bgr_y], dx
3536 clevermous 2823
        jae     @f
9941 Doczom 2824
        mov     word [edi + WDATA.draw_bgr_y], dx
3536 clevermous 2825
@@:
3611 clevermous 2826
        shr     edx, 16
9941 Doczom 2827
        cmp     word [edi + WDATA.draw_bgr_y+2], dx
3536 clevermous 2828
        jbe     @f
9941 Doczom 2829
        mov     word [edi + WDATA.draw_bgr_y+2], dx
3536 clevermous 2830
@@:
2831
        jmp     .common
2832
.set:
9941 Doczom 2833
        mov     [edi + WDATA.draw_bgr_x], eax
2834
        mov     [edi + WDATA.draw_bgr_y], edx
3536 clevermous 2835
.common:
9941 Doczom 2836
        mov     eax, [edi + WDATA.thread]
9930 Doczom 2837
        test    eax, eax
2838
        jz      @f
2839
        or      [eax + APPDATA.occurred_events], EVENT_BACKGROUND
2840
@@:
9941 Doczom 2841
        loop    set_bgr_event
2513 mario79 2842
        pop     edi ecx
2843
;--------- set event 5 stop -----------
6585 pathoswith 2844
        dec     [REDRAW_BACKGROUND]     ; got new update request?
3536 clevermous 2845
        jnz     backgr
2513 mario79 2846
 
3536 clevermous 2847
        xor     eax, eax
9930 Doczom 2848
        mov     [background_window + WDATA.draw_data.left], eax
2849
        mov     [background_window + WDATA.draw_data.top], eax
2850
        mov     [background_window + WDATA.draw_data.right], eax
2851
        mov     [background_window + WDATA.draw_data.bottom], eax
2513 mario79 2852
;--------------------------------------
2853
align 4
2288 clevermous 2854
nobackgr:
2513 mario79 2855
; system shutdown request
2288 clevermous 2856
        cmp     [SYS_SHUTDOWN], byte 0
2857
        je      noshutdown
2858
 
2859
        mov     edx, [shutdown_processes]
2860
 
2861
        cmp     [SYS_SHUTDOWN], dl
3325 clevermous 2862
        jne     noshutdown
2288 clevermous 2863
 
2864
        lea     ecx, [edx-1]
9941 Doczom 2865
        mov     edx, SLOT_BASE + sizeof.APPDATA*2 ;OS_BASE+0x3040
3325 clevermous 2866
        jecxz   no_mark_system_shutdown
2513 mario79 2867
;--------------------------------------
2868
align 4
2288 clevermous 2869
markz:
3296 clevermous 2870
        push    ecx edx
9709 Doczom 2871
        cmp     [edx + APPDATA.state], TSTATE_FREE
3325 clevermous 2872
        jz      .nokill
9715 Doczom 2873
        cmp     [edx + APPDATA.process], sys_proc
3325 clevermous 2874
        jz      .nokill
3296 clevermous 2875
        call    request_terminate
3325 clevermous 2876
        jmp     .common
2877
.nokill:
2878
        dec     byte [SYS_SHUTDOWN]
2879
        xor     eax, eax
2880
.common:
3296 clevermous 2881
        pop     edx ecx
2882
        test    eax, eax
2883
        jz      @f
9709 Doczom 2884
        mov     [edx + APPDATA.state], TSTATE_ZOMBIE
3296 clevermous 2885
@@:
9709 Doczom 2886
        add     edx, sizeof.APPDATA
2288 clevermous 2887
        loop    markz
3534 clevermous 2888
        call    wakeup_osloop
2513 mario79 2889
;--------------------------------------
2890
align 4
2288 clevermous 2891
@@:
2513 mario79 2892
no_mark_system_shutdown:
2288 clevermous 2893
        dec     byte [SYS_SHUTDOWN]
2894
        je      system_shutdown
2513 mario79 2895
;--------------------------------------
2896
align 4
2288 clevermous 2897
noshutdown:
8866 rgimad 2898
        mov     eax, [thread_count]           ; termination
9709 Doczom 2899
        mov     ebx, SLOT_BASE + sizeof.APPDATA + APPDATA.state
2288 clevermous 2900
        mov     esi, 1
2513 mario79 2901
;--------------------------------------
2902
align 4
2288 clevermous 2903
newct:
2904
        mov     cl, [ebx]
9715 Doczom 2905
        cmp     cl, TSTATE_ZOMBIE
3325 clevermous 2906
        jz      .terminate
2513 mario79 2907
 
9715 Doczom 2908
        cmp     cl, TSTATE_TERMINATING
3325 clevermous 2909
        jnz     .noterminate
2910
.terminate:
2911
        pushad
9941 Doczom 2912
        push    esi
2913
        mov     ecx, dword[ebx - APPDATA.state + APPDATA.window]
3493 mario79 2914
        call    restore_default_cursor_before_killing
2915
 
9941 Doczom 2916
        pop     esi
3325 clevermous 2917
        call    terminate
2918
        popad
2919
        cmp     byte [SYS_SHUTDOWN], 0
2920
        jz      .noterminate
2921
        dec     byte [SYS_SHUTDOWN]
2922
        je      system_shutdown
2288 clevermous 2923
 
3325 clevermous 2924
.noterminate:
9709 Doczom 2925
        add     ebx, sizeof.APPDATA
2288 clevermous 2926
        inc     esi
2927
        dec     eax
2928
        jnz     newct
2929
        ret
2513 mario79 2930
;-----------------------------------------------------------------------------
2931
align 4
9941 Doczom 2932
; eax - ptr to WDATA
2288 clevermous 2933
redrawscreen:
2934
; eax , if process window_data base is eax, do not set flag/limits
2935
 
2936
        pushad
2937
        push    eax
2938
 
2939
;;;         mov   ebx,2
2940
;;;         call  delay_hs
2941
 
2942
         ;mov   ecx,0               ; redraw flags for apps
2943
        xor     ecx, ecx
2513 mario79 2944
;--------------------------------------
2945
align 4
2946
newdw2:
2288 clevermous 2947
        inc     ecx
2948
        push    ecx
2949
 
2950
        mov     eax, ecx
9715 Doczom 2951
        shl     eax, BSF sizeof.WDATA
2288 clevermous 2952
        add     eax, window_data
2953
 
2954
        cmp     eax, [esp+4]
2955
        je      not_this_task
2956
                                   ; check if window in redraw area
2957
        mov     edi, eax
2958
 
2959
        cmp     ecx, 1             ; limit for background
2960
        jz      bgli
2961
 
5870 GerdtR 2962
        mov     eax, [esp+4]        ;if upper in z-position - no redraw
2963
        test    eax, eax
2964
        jz      @f
2965
        mov     al, [eax + WDATA.z_modif]
2966
        cmp     [edi + WDATA.z_modif], al
2967
        jg      ricino
2968
      @@:
2969
 
2288 clevermous 2970
        mov     eax, [edi + WDATA.box.left]
2971
        mov     ebx, [edi + WDATA.box.top]
2972
 
9941 Doczom 2973
        cmp     ebx, [draw_limits.bottom] ; ecx = area y end     ebx = window y start
2974
        jae     ricino
2288 clevermous 2975
 
9941 Doczom 2976
        cmp     eax, [draw_limits.right] ; ecx = area x end     eax = window x start
2977
        jae     ricino
2288 clevermous 2978
 
2979
        mov     eax, [edi + WDATA.box.left]
2980
        mov     ebx, [edi + WDATA.box.top]
2981
        mov     ecx, [edi + WDATA.box.width]
2982
        mov     edx, [edi + WDATA.box.height]
2983
        add     ecx, eax
2984
        add     edx, ebx
2985
 
2986
        mov     eax, [draw_limits.top]  ; eax = area y start     edx = window y end
2987
        cmp     edx, eax
2988
        jb      ricino
2989
 
2990
        mov     eax, [draw_limits.left]  ; eax = area x start     ecx = window x end
2991
        cmp     ecx, eax
2992
        jb      ricino
2513 mario79 2993
;--------------------------------------
2994
align 4
2995
bgli:
9941 Doczom 2996
        cmp     dword[esp], 1  ; check index in window_data array, 1 - idle
2288 clevermous 2997
        jnz     .az
2513 mario79 2998
 
6585 pathoswith 2999
        cmp     [REDRAW_BACKGROUND], 0
2288 clevermous 3000
        jz      .az
2513 mario79 3001
 
2288 clevermous 3002
        mov     dl, 0
3003
        mov     ebx, [draw_limits.left]
9930 Doczom 3004
        cmp     ebx, [edi + WDATA.draw_data.left]
2288 clevermous 3005
        jae     @f
2513 mario79 3006
 
9930 Doczom 3007
        mov     [edi + WDATA.draw_data.left], ebx
2288 clevermous 3008
        mov     dl, 1
2513 mario79 3009
;--------------------------------------
3010
align 4
3011
@@:
2288 clevermous 3012
        mov     ebx, [draw_limits.top]
9930 Doczom 3013
        cmp     ebx, [edi + WDATA.draw_data.top]
2288 clevermous 3014
        jae     @f
2513 mario79 3015
 
9930 Doczom 3016
        mov     [edi + WDATA.draw_data.top], ebx
2288 clevermous 3017
        mov     dl, 1
2513 mario79 3018
;--------------------------------------
3019
align 4
3020
@@:
2288 clevermous 3021
        mov     ebx, [draw_limits.right]
9930 Doczom 3022
        cmp     ebx, [edi + WDATA.draw_data.right]
2288 clevermous 3023
        jbe     @f
2513 mario79 3024
 
9930 Doczom 3025
        mov     [edi + WDATA.draw_data.right], ebx
2288 clevermous 3026
        mov     dl, 1
2513 mario79 3027
;--------------------------------------
3028
align 4
3029
@@:
2288 clevermous 3030
        mov     ebx, [draw_limits.bottom]
9930 Doczom 3031
        cmp     ebx, [edi + WDATA.draw_data.bottom]
2288 clevermous 3032
        jbe     @f
2513 mario79 3033
 
9930 Doczom 3034
        mov     [edi + WDATA.draw_data.bottom], ebx
2288 clevermous 3035
        mov     dl, 1
2513 mario79 3036
;--------------------------------------
3037
align 4
3038
@@:
6585 pathoswith 3039
        add     [REDRAW_BACKGROUND], dl
3534 clevermous 3040
        call    wakeup_osloop
2288 clevermous 3041
        jmp     newdw8
2513 mario79 3042
;--------------------------------------
3043
align 4
3044
.az:
2288 clevermous 3045
        mov     ebx, [draw_limits.left]        ; set limits
9941 Doczom 3046
        mov     [edi + WDATA.draw_data.left], ebx
2288 clevermous 3047
        mov     ebx, [draw_limits.top]
9941 Doczom 3048
        mov     [edi + WDATA.draw_data.top], ebx
2288 clevermous 3049
        mov     ebx, [draw_limits.right]
9941 Doczom 3050
        mov     [edi + WDATA.draw_data.right], ebx
2288 clevermous 3051
        mov     ebx, [draw_limits.bottom]
9941 Doczom 3052
        mov     [edi + WDATA.draw_data.bottom], ebx
2288 clevermous 3053
 
9941 Doczom 3054
        cmp     dword [esp], 1  ; check idle thread
2288 clevermous 3055
        jne     nobgrd
6585 pathoswith 3056
        inc     [REDRAW_BACKGROUND]
3534 clevermous 3057
        call    wakeup_osloop
2513 mario79 3058
;--------------------------------------
3059
align 4
3060
newdw8:
3061
nobgrd:
3936 mario79 3062
;--------------------------------------
9941 Doczom 3063
        push    edi ebp
3064
        mov     edi, [esp+8]
3936 mario79 3065
        cmp     edi, 1
3066
        je      .found
2288 clevermous 3067
 
3936 mario79 3068
        mov     eax, [draw_limits.left]
3069
        mov     ebx, [draw_limits.top]
3070
        mov     ecx, [draw_limits.right]
3071
        sub     ecx, eax
3072
        test    ecx, ecx
3073
        jz      .not_found
3074
 
3075
        mov     edx, [draw_limits.bottom]
3076
        sub     edx, ebx
3077
        test    edx, edx
3078
        jz      .not_found
3079
 
3080
; eax - x, ebx - y
3081
; ecx - size x, edx - size y
3082
        add     ebx, edx
3083
;--------------------------------------
3084
align 4
3085
.start_y:
3086
        push    ecx
3087
;--------------------------------------
3088
align 4
3089
.start_x:
3090
        add     eax, ecx
3091
        mov     ebp, [d_width_calc_area + ebx*4]
5351 serge 3092
        add     ebp, [_display.win_map]
3936 mario79 3093
        movzx   ebp, byte[eax+ebp] ; get value for current point
3094
        cmp     ebp, edi
3095
        jne     @f
3096
 
3097
        pop     ecx
3098
        jmp     .found
3099
;--------------------------------------
3100
align 4
3101
@@:
3102
        sub     eax, ecx
3103
 
3104
        dec     ecx
9221 dunkaist 3105
        jns     .start_x
3936 mario79 3106
 
3107
        pop     ecx
3108
        dec     ebx
3109
        dec     edx
9221 dunkaist 3110
        jns     .start_y
3936 mario79 3111
;--------------------------------------
3112
align 4
3113
.not_found:
9941 Doczom 3114
        pop     ebp edi
3936 mario79 3115
        jmp     ricino
3116
;--------------------------------------
3117
align 4
3118
.found:
9941 Doczom 3119
        pop     ebp edi
3936 mario79 3120
 
9941 Doczom 3121
        mov     [edi + WDATA.fl_redraw], WSTATE_REDRAW  ; mark as redraw
2513 mario79 3122
;--------------------------------------
3123
align 4
3124
ricino:
3125
not_this_task:
2288 clevermous 3126
        pop     ecx
3127
 
8866 rgimad 3128
        cmp     ecx, [thread_count]
2288 clevermous 3129
        jle     newdw2
3130
 
3131
        pop     eax
3132
        popad
3133
        ret
2513 mario79 3134
;-----------------------------------------------------------------------------
3135
align 4
2288 clevermous 3136
calculatebackground:   ; background
5351 serge 3137
        mov     edi, [_display.win_map]              ; set os to use all pixels
2288 clevermous 3138
        mov     eax, 0x01010101
5351 serge 3139
        mov     ecx, [_display.win_map_size]
2288 clevermous 3140
        shr     ecx, 2
3141
        rep stosd
9930 Doczom 3142
        mov     byte[background_window + WDATA.z_modif], ZPOS_DESKTOP
6585 pathoswith 3143
        mov     [REDRAW_BACKGROUND], 0
2288 clevermous 3144
        ret
2513 mario79 3145
;-----------------------------------------------------------------------------
2288 clevermous 3146
uglobal
3147
  imax    dd 0x0
3148
endg
2513 mario79 3149
;-----------------------------------------------------------------------------
3150
align 4
2288 clevermous 3151
delay_ms:     ; delay in 1/1000 sec
8139 dunkaist 3152
        pushad
3153
 
3154
        cmp     [hpet_base], 0
3155
        jz      .no_hpet
3156
        mov     eax, esi
8176 dunkaist 3157
        mov     edx, 1_000_000 ; ms to ns
8139 dunkaist 3158
        mul     edx
3159
        mov     ebx, edx
3160
        mov     ecx, eax
3161
 
2288 clevermous 3162
        push    ecx
8139 dunkaist 3163
        call    get_clock_ns
3164
        pop     ecx
3165
        mov     edi, edx
3166
        mov     esi, eax
3167
.wait:
3168
        push    ecx
3169
        call    get_clock_ns
3170
        pop     ecx
3171
        sub     eax, esi
3172
        sbb     edx, edi
3173
        sub     eax, ecx
3174
        sbb     edx, ebx
3175
        jc      .wait
3176
        jmp     .done
2288 clevermous 3177
 
8139 dunkaist 3178
.no_hpet:
2288 clevermous 3179
        mov     ecx, esi
3180
        ; 
3181
        imul    ecx, 33941
3182
        shr     ecx, 9
3183
        ; 
3184
 
3185
        in      al, 0x61
3186
        and     al, 0x10
3187
        mov     ah, al
3188
        cld
8139 dunkaist 3189
 
3190
.cnt1:
2288 clevermous 3191
        in      al, 0x61
3192
        and     al, 0x10
3193
        cmp     al, ah
8139 dunkaist 3194
        jz      .cnt1
2288 clevermous 3195
 
3196
        mov     ah, al
8139 dunkaist 3197
        loop    .cnt1
2288 clevermous 3198
 
8139 dunkaist 3199
.done:
3200
        popad
2288 clevermous 3201
        ret
2513 mario79 3202
;-----------------------------------------------------------------------------
2411 Serge 3203
align 4
2288 clevermous 3204
set_app_param:
9612 Doczom 3205
        mov     edi, [current_slot]
9614 Doczom 3206
        xchg    ebx, [edi + APPDATA.event_mask] ; set new event mask
9897 dunkaist 3207
        mov     [esp + SYSCALL_STACK.eax], ebx  ; return old mask value
2288 clevermous 3208
        ret
2513 mario79 3209
;-----------------------------------------------------------------------------
3303 clevermous 3210
 
3211
; this is for syscall
3212
proc delay_hs_unprotected
3213
        call    unprotect_from_terminate
3214
        call    delay_hs
3215
        call    protect_from_terminate
3216
        ret
3217
endp
3218
 
3597 Serge 3219
if 1
2513 mario79 3220
align 4
2288 clevermous 3221
delay_hs:     ; delay in 1/100 secs
3222
; ebx = delay time
3595 Serge 3223
        pushad
3224
        push    ebx
3225
        xor     esi, esi
3226
        mov     ecx, MANUAL_DESTROY
3227
        call    create_event
3228
        test    eax, eax
3229
        jz      .done
2288 clevermous 3230
 
3595 Serge 3231
        mov     ebx, edx
3232
        mov     ecx, [esp]
3597 Serge 3233
        push    edx
3595 Serge 3234
        push    eax
3235
        call    wait_event_timeout
3236
        pop     eax
3597 Serge 3237
        pop     ebx
3595 Serge 3238
        call    destroy_event
3239
.done:
3240
        add     esp, 4
3241
        popad
2288 clevermous 3242
        ret
3596 Serge 3243
 
3244
else
3245
 
3246
align 4
3247
delay_hs:     ; delay in 1/100 secs
3248
; ebx = delay time
3249
        push    ecx
3250
        push    edx
3251
 
3252
        mov     edx, [timer_ticks]
3253
;--------------------------------------
3254
align 4
9910 Doczom 3255
.newtic:
3596 Serge 3256
        mov     ecx, [timer_ticks]
3257
        sub     ecx, edx
3258
        cmp     ecx, ebx
9910 Doczom 3259
        jae     .zerodelay
3596 Serge 3260
 
3261
        call    change_task
3262
 
9910 Doczom 3263
        jmp     .newtic
3596 Serge 3264
;--------------------------------------
3265
align 4
9910 Doczom 3266
.zerodelay:
3596 Serge 3267
        pop     edx
3268
        pop     ecx
3269
        ret
3270
end if
3271
 
2513 mario79 3272
;-----------------------------------------------------------------------------
2288 clevermous 3273
align 16        ;very often call this subrutine
3274
memmove:       ; memory move in bytes
3275
; eax = from
3276
; ebx = to
3277
; ecx = no of bytes
3278
        test    ecx, ecx
3279
        jle     .ret
3280
 
3281
        push    esi edi ecx
3282
 
3283
        mov     edi, ebx
3284
        mov     esi, eax
3285
 
3286
        test    ecx, not 11b
3287
        jz      @f
3288
 
3289
        push    ecx
3290
        shr     ecx, 2
3291
        rep movsd
3292
        pop     ecx
3293
        and     ecx, 11b
3294
        jz      .finish
2513 mario79 3295
;--------------------------------------
3296
align 4
3297
@@:
2288 clevermous 3298
        rep movsb
2513 mario79 3299
;--------------------------------------
3300
align 4
3301
.finish:
2288 clevermous 3302
        pop     ecx edi esi
2513 mario79 3303
;--------------------------------------
3304
align 4
3305
.ret:
2288 clevermous 3306
        ret
2513 mario79 3307
;-----------------------------------------------------------------------------
2288 clevermous 3308
 
9742 Doczom 3309
; in: eax = port
3310
;     ebp = subfunction
3311
;          0 - set access
3312
;          1 - clear access
3313
; out: not return value
2288 clevermous 3314
align 4
3315
set_io_access_rights:
3316
        push    edi eax
3317
        mov     edi, tss._io_map_0
9743 Doczom 3318
 
9742 Doczom 3319
        test    ebp, ebp         ; enable access - ebp = 0
2288 clevermous 3320
        jnz     .siar1
9743 Doczom 3321
 
2288 clevermous 3322
        btr     [edi], eax
3323
        pop     eax edi
3324
        ret
3325
.siar1:
9742 Doczom 3326
        bts     [edi], eax      ; disable access - ebp = 1
2288 clevermous 3327
        pop     eax edi
3328
        ret
9279 Doczom 3329
 
3330
align 4
9743 Doczom 3331
; @brief ReservePortArea and FreePortArea
3332
; @param edx number end arrea of ports (include last number of port)
3333
; @param ecx number start arrea of ports
3334
; @param ebx sub function 0 - reserve, 1 - free
3335
; @param eax 46 - number function
3336
; @returns  eax = 0 - succesful eax = 1 - error
3337
syscall_reserveportarea:        ; ReservePortArea and FreePortArea
9279 Doczom 3338
 
3339
        call    r_f_port_area
9831 dunkaist 3340
        mov     [esp + SYSCALL_STACK.eax], eax
9279 Doczom 3341
        ret
3342
 
2288 clevermous 3343
;reserve/free group of ports
3344
;  * eax = 46 - number function
3345
;  * ebx = 0 - reserve, 1 - free
3346
;  * ecx = number start arrea of ports
3347
;  * edx = number end arrea of ports (include last number of port)
3348
;Return value:
3349
;  * eax = 0 - succesful
3350
;  * eax = 1 - error
3351
;  * The system has reserve this ports:
3352
;    0..0x2d, 0x30..0x4d, 0x50..0xdf, 0xe5..0xff (include last number of port).
3353
;destroys eax,ebx, ebp
3354
r_f_port_area:
3355
 
3356
        test    ebx, ebx
9910 Doczom 3357
        jnz     .free_port_area
2288 clevermous 3358
 
3359
        cmp     ecx, edx      ; beginning > end ?
9917 Doczom 3360
        ja      .err
9743 Doczom 3361
        cmp     edx, 65536    ;test ebx, not 0xffff
9917 Doczom 3362
        jae     .err
2288 clevermous 3363
        mov     eax, [RESERVED_PORTS]
3364
        test    eax, eax      ; no reserved areas ?
9910 Doczom 3365
        je      .rpal2
2288 clevermous 3366
        cmp     eax, 255      ; max reserved
9917 Doczom 3367
        jae     .err
9910 Doczom 3368
 .rpal3:
2288 clevermous 3369
        mov     ebx, eax
9743 Doczom 3370
        shl     ebx, 4   ;16 byte is sizeof item in RESERVED_PORTS table
2288 clevermous 3371
        add     ebx, RESERVED_PORTS
3372
        cmp     ecx, [ebx+8]
9910 Doczom 3373
        ja      .rpal4
2288 clevermous 3374
        cmp     edx, [ebx+4]
9917 Doczom 3375
        jae     .err
9910 Doczom 3376
 .rpal4:
2288 clevermous 3377
        dec     eax
9910 Doczom 3378
        jnz     .rpal3
3379
        jmp     .rpal2
9917 Doczom 3380
.err:
2288 clevermous 3381
        xor     eax, eax
3382
        inc     eax
3383
        ret
9917 Doczom 3384
 
9910 Doczom 3385
   .rpal2:
2288 clevermous 3386
     ; enable port access at port IO map
3387
        pushad                        ; start enable io map
3388
        mov     eax, ecx
3389
        xor     ebp, ebp               ; enable - eax = port
9742 Doczom 3390
        cli
9910 Doczom 3391
.new_port_access:
2288 clevermous 3392
        call    set_io_access_rights
9742 Doczom 3393
 
2288 clevermous 3394
        inc     eax
3395
        cmp     eax, edx
9910 Doczom 3396
        jbe     .new_port_access
9917 Doczom 3397
 
9742 Doczom 3398
        sti
2288 clevermous 3399
        popad                         ; end enable io map
3400
 
3401
        mov     eax, [RESERVED_PORTS]
9742 Doczom 3402
        inc     eax
2288 clevermous 3403
        mov     [RESERVED_PORTS], eax
3404
        shl     eax, 4
3405
        add     eax, RESERVED_PORTS
9692 Doczom 3406
        mov     ebx, [current_slot]
9715 Doczom 3407
        mov     ebx, [ebx + APPDATA.tid]
9743 Doczom 3408
        mov     [eax], ebx   ; tid
3409
        mov     [eax+4], ecx ;start port
3410
        mov     [eax+8], edx ;finish port
2288 clevermous 3411
 
3412
        xor     eax, eax
3413
        ret
3414
 
9910 Doczom 3415
.free_port_area:
2288 clevermous 3416
 
3417
        mov     eax, [RESERVED_PORTS]; no reserved areas ?
3418
        test    eax, eax
9910 Doczom 3419
        jz      .frpal2
9692 Doczom 3420
        mov     ebx, [current_slot]
9715 Doczom 3421
        mov     ebx, [ebx + APPDATA.tid]
9910 Doczom 3422
   .frpal3:
2288 clevermous 3423
        mov     edi, eax
3424
        shl     edi, 4
3425
        add     edi, RESERVED_PORTS
3426
        cmp     ebx, [edi]
9910 Doczom 3427
        jne     .frpal4
2288 clevermous 3428
        cmp     ecx, [edi+4]
9910 Doczom 3429
        jne     .frpal4
2288 clevermous 3430
        cmp     edx, [edi+8]
9910 Doczom 3431
        jne     .frpal4
3432
        jmp     .frpal1
3433
   .frpal4:
2288 clevermous 3434
        dec     eax
9910 Doczom 3435
        jnz     .frpal3
3436
   .frpal2:
2288 clevermous 3437
        inc     eax
3438
        ret
9910 Doczom 3439
   .frpal1:
2288 clevermous 3440
        push    ecx
3441
        mov     ecx, 256
3442
        sub     ecx, eax
3443
        shl     ecx, 4
3444
        mov     esi, edi
3445
        add     esi, 16
3446
        cld
3447
        rep movsb
3448
 
3449
        dec     dword [RESERVED_PORTS]
3450
;disable port access at port IO map
9742 Doczom 3451
                        ; start disable io map
2288 clevermous 3452
        pop     eax     ;start port
9742 Doczom 3453
        ;cmp     edx, 65536
3454
        ;jge     no_mask_io
2288 clevermous 3455
 
3456
        xor     ebp, ebp
3457
        inc     ebp
9910 Doczom 3458
.new_port_access_disable:           ; disable - eax = port
2288 clevermous 3459
        call    set_io_access_rights
9742 Doczom 3460
 
2288 clevermous 3461
        inc     eax
3462
        cmp     eax, edx
9910 Doczom 3463
        jbe     .new_port_access_disable
9917 Doczom 3464
                                    ; end disable io map
2288 clevermous 3465
        xor     eax, eax
3466
        ret
2430 mario79 3467
;-----------------------------------------------------------------------------
2288 clevermous 3468
align 4
3469
drawbackground:
3470
        cmp     [BgrDrawMode], dword 1
9910 Doczom 3471
        jne     .bgrstr
2288 clevermous 3472
        call    vesa20_drawbackground_tiled
2453 mario79 3473
        call    __sys_draw_pointer
2288 clevermous 3474
        ret
2430 mario79 3475
;--------------------------------------
3476
align 4
9910 Doczom 3477
.bgrstr:
2288 clevermous 3478
        call    vesa20_drawbackground_stretch
2453 mario79 3479
        call    __sys_draw_pointer
2288 clevermous 3480
        ret
2430 mario79 3481
;-----------------------------------------------------------------------------
2288 clevermous 3482
align 4
3483
syscall_putimage:                       ; PutImage
8714 Doczom 3484
; add check pointer
3485
        push    ecx
3486
        mov     ax, cx
3487
        shr     ecx, 16
3488
        imul    eax, ecx
3489
        lea     eax, [eax*3]
3490
        stdcall is_region_userspace, ebx, eax
3491
        pop     ecx
9045 dunkaist 3492
        jnz     sys_putimage.exit
8714 Doczom 3493
 
2288 clevermous 3494
sys_putimage:
3495
        test    ecx, 0x80008000
3496
        jnz     .exit
3497
        test    ecx, 0x0000FFFF
3498
        jz      .exit
3499
        test    ecx, 0xFFFF0000
3500
        jnz     @f
2430 mario79 3501
;--------------------------------------
3502
align 4
3503
.exit:
2288 clevermous 3504
        ret
2430 mario79 3505
;--------------------------------------
3506
align 4
3507
@@:
9948 Doczom 3508
        mov     edi, [current_slot]
9949 Doczom 3509
        mov     edi, [edi + APPDATA.window]
9948 Doczom 3510
        add     dx, word[edi + WDATA.clientbox.top]
2288 clevermous 3511
        rol     edx, 16
9948 Doczom 3512
        add     dx, word[edi + WDATA.clientbox.left]
2288 clevermous 3513
        rol     edx, 16
2430 mario79 3514
;--------------------------------------
3515
align 4
3516
.forced:
2288 clevermous 3517
        push    ebp esi 0
3518
        mov     ebp, putimage_get24bpp
3519
        mov     esi, putimage_init24bpp
2430 mario79 3520
;--------------------------------------
3521
align 4
2288 clevermous 3522
sys_putimage_bpp:
2430 mario79 3523
        call    vesa20_putimage
2288 clevermous 3524
        pop     ebp esi ebp
2430 mario79 3525
        ret
3526
;        jmp     [draw_pointer]
3527
;-----------------------------------------------------------------------------
2288 clevermous 3528
align 4
3529
sys_putimage_palette:
3530
; ebx = pointer to image
3531
; ecx = [xsize]*65536 + [ysize]
3532
; edx = [xstart]*65536 + [ystart]
3533
; esi = number of bits per pixel, must be 8, 24 or 32
3534
; edi = pointer to palette
3535
; ebp = row delta
8715 Doczom 3536
; check pointer
9910 Doczom 3537
        push    ecx esi
8715 Doczom 3538
        mov     ax, cx
3539
        shr     ecx, 16
3540
        imul    eax, ecx
9910 Doczom 3541
;        imul    eax, esi ; eax*count bit in 1 pixel
3542
;        shr     eax, 3
8715 Doczom 3543
        stdcall is_region_userspace, ebx, eax
9910 Doczom 3544
        pop     esi ecx
9045 dunkaist 3545
        jnz     sys_putimage.exit
8715 Doczom 3546
 
9949 Doczom 3547
        mov     eax, [current_slot]
3548
        mov     eax, [eax + APPDATA.window]
3549
        add     dx, word [eax + WDATA.clientbox.top]
2288 clevermous 3550
        rol     edx, 16
9949 Doczom 3551
        add     dx, word [eax + WDATA.clientbox.left]
2288 clevermous 3552
        rol     edx, 16
2430 mario79 3553
;--------------------------------------
3554
align 4
2288 clevermous 3555
.forced:
3556
        cmp     esi, 1
3557
        jnz     @f
3558
        push    edi
3559
        mov     eax, [edi+4]
3560
        sub     eax, [edi]
3561
        push    eax
3562
        push    dword [edi]
3563
        push    0ffffff80h
3564
        mov     edi, esp
3565
        call    put_mono_image
3566
        add     esp, 12
3567
        pop     edi
3568
        ret
2430 mario79 3569
;--------------------------------------
3570
align 4
2288 clevermous 3571
@@:
3572
        cmp     esi, 2
3573
        jnz     @f
3574
        push    edi
3575
        push    0ffffff80h
3576
        mov     edi, esp
3577
        call    put_2bit_image
3578
        pop     eax
3579
        pop     edi
3580
        ret
2430 mario79 3581
;--------------------------------------
3582
align 4
2288 clevermous 3583
@@:
3584
        cmp     esi, 4
3585
        jnz     @f
3586
        push    edi
3587
        push    0ffffff80h
3588
        mov     edi, esp
3589
        call    put_4bit_image
3590
        pop     eax
3591
        pop     edi
3592
        ret
2430 mario79 3593
;--------------------------------------
3594
align 4
2288 clevermous 3595
@@:
3596
        push    ebp esi ebp
3597
        cmp     esi, 8
3598
        jnz     @f
3599
        mov     ebp, putimage_get8bpp
3600
        mov     esi, putimage_init8bpp
3601
        jmp     sys_putimage_bpp
2430 mario79 3602
;--------------------------------------
3603
align 4
2288 clevermous 3604
@@:
2727 dunkaist 3605
        cmp     esi, 9
3606
        jnz     @f
3607
        mov     ebp, putimage_get9bpp
3608
        mov     esi, putimage_init9bpp
3609
        jmp     sys_putimage_bpp
3610
;--------------------------------------
3611
align 4
3612
@@:
2288 clevermous 3613
        cmp     esi, 15
3614
        jnz     @f
3615
        mov     ebp, putimage_get15bpp
3616
        mov     esi, putimage_init15bpp
3617
        jmp     sys_putimage_bpp
2430 mario79 3618
;--------------------------------------
3619
align 4
2288 clevermous 3620
@@:
3621
        cmp     esi, 16
3622
        jnz     @f
3623
        mov     ebp, putimage_get16bpp
3624
        mov     esi, putimage_init16bpp
3625
        jmp     sys_putimage_bpp
2430 mario79 3626
;--------------------------------------
3627
align 4
2288 clevermous 3628
@@:
3629
        cmp     esi, 24
3630
        jnz     @f
3631
        mov     ebp, putimage_get24bpp
3632
        mov     esi, putimage_init24bpp
3633
        jmp     sys_putimage_bpp
2430 mario79 3634
;--------------------------------------
3635
align 4
2288 clevermous 3636
@@:
3637
        cmp     esi, 32
3638
        jnz     @f
3639
        mov     ebp, putimage_get32bpp
3640
        mov     esi, putimage_init32bpp
3641
        jmp     sys_putimage_bpp
2430 mario79 3642
;--------------------------------------
3643
align 4
2288 clevermous 3644
@@:
3645
        pop     ebp esi ebp
3646
        ret
2430 mario79 3647
;-----------------------------------------------------------------------------
3648
align 4
2288 clevermous 3649
put_mono_image:
3650
        push    ebp esi ebp
3651
        mov     ebp, putimage_get1bpp
3652
        mov     esi, putimage_init1bpp
3653
        jmp     sys_putimage_bpp
2430 mario79 3654
;-----------------------------------------------------------------------------
3655
align 4
2288 clevermous 3656
put_2bit_image:
3657
        push    ebp esi ebp
3658
        mov     ebp, putimage_get2bpp
3659
        mov     esi, putimage_init2bpp
3660
        jmp     sys_putimage_bpp
2430 mario79 3661
;-----------------------------------------------------------------------------
3662
align 4
2288 clevermous 3663
put_4bit_image:
3664
        push    ebp esi ebp
3665
        mov     ebp, putimage_get4bpp
3666
        mov     esi, putimage_init4bpp
3667
        jmp     sys_putimage_bpp
2430 mario79 3668
;-----------------------------------------------------------------------------
3669
align 4
2288 clevermous 3670
putimage_init24bpp:
3671
        lea     eax, [eax*3]
3672
putimage_init8bpp:
2727 dunkaist 3673
putimage_init9bpp:
2288 clevermous 3674
        ret
2430 mario79 3675
;-----------------------------------------------------------------------------
2288 clevermous 3676
align 16
3677
putimage_get24bpp:
3678
        movzx   eax, byte [esi+2]
3679
        shl     eax, 16
3680
        mov     ax, [esi]
3681
        add     esi, 3
3682
        ret     4
2430 mario79 3683
;-----------------------------------------------------------------------------
2288 clevermous 3684
align 16
3685
putimage_get8bpp:
3686
        movzx   eax, byte [esi]
3687
        push    edx
3688
        mov     edx, [esp+8]
9715 Doczom 3689
        mov     eax, [edx + eax*4]
2288 clevermous 3690
        pop     edx
3691
        inc     esi
3692
        ret     4
2430 mario79 3693
;-----------------------------------------------------------------------------
2727 dunkaist 3694
align 16
3695
putimage_get9bpp:
3696
        lodsb
3697
        mov     ah, al
3698
        shl     eax, 8
3699
        mov     al, ah
3700
        ret     4
3701
;-----------------------------------------------------------------------------
2430 mario79 3702
align 4
2288 clevermous 3703
putimage_init1bpp:
3704
        add     eax, ecx
3705
        push    ecx
3706
        add     eax, 7
3707
        add     ecx, 7
3708
        shr     eax, 3
3709
        shr     ecx, 3
3710
        sub     eax, ecx
3711
        pop     ecx
3712
        ret
2430 mario79 3713
;-----------------------------------------------------------------------------
2288 clevermous 3714
align 16
3715
putimage_get1bpp:
3716
        push    edx
3717
        mov     edx, [esp+8]
3718
        mov     al, [edx]
3719
        add     al, al
3720
        jnz     @f
3721
        lodsb
3722
        adc     al, al
3723
@@:
3724
        mov     [edx], al
3725
        sbb     eax, eax
3726
        and     eax, [edx+8]
3727
        add     eax, [edx+4]
3728
        pop     edx
3729
        ret     4
2430 mario79 3730
;-----------------------------------------------------------------------------
3731
align 4
2288 clevermous 3732
putimage_init2bpp:
3733
        add     eax, ecx
3734
        push    ecx
3735
        add     ecx, 3
3736
        add     eax, 3
3737
        shr     ecx, 2
3738
        shr     eax, 2
3739
        sub     eax, ecx
3740
        pop     ecx
3741
        ret
2430 mario79 3742
;-----------------------------------------------------------------------------
2288 clevermous 3743
align 16
3744
putimage_get2bpp:
3745
        push    edx
3746
        mov     edx, [esp+8]
3747
        mov     al, [edx]
3748
        mov     ah, al
3749
        shr     al, 6
3750
        shl     ah, 2
3751
        jnz     .nonewbyte
3752
        lodsb
3753
        mov     ah, al
3754
        shr     al, 6
3755
        shl     ah, 2
3756
        add     ah, 1
3757
.nonewbyte:
3758
        mov     [edx], ah
3759
        mov     edx, [edx+4]
3760
        movzx   eax, al
9715 Doczom 3761
        mov     eax, [edx + eax*4]
2288 clevermous 3762
        pop     edx
3763
        ret     4
2430 mario79 3764
;-----------------------------------------------------------------------------
3765
align 4
2288 clevermous 3766
putimage_init4bpp:
3767
        add     eax, ecx
3768
        push    ecx
3769
        add     ecx, 1
9268 Doczom 3770
        inc     eax      ;add   eax, 1
2288 clevermous 3771
        shr     ecx, 1
3772
        shr     eax, 1
3773
        sub     eax, ecx
3774
        pop     ecx
3775
        ret
2430 mario79 3776
;-----------------------------------------------------------------------------
2288 clevermous 3777
align 16
3778
putimage_get4bpp:
3779
        push    edx
3780
        mov     edx, [esp+8]
3781
        add     byte [edx], 80h
3782
        jc      @f
3783
        movzx   eax, byte [edx+1]
3784
        mov     edx, [edx+4]
3785
        and     eax, 0x0F
9715 Doczom 3786
        mov     eax, [edx + eax*4]
2288 clevermous 3787
        pop     edx
3788
        ret     4
3789
@@:
3790
        movzx   eax, byte [esi]
3791
        add     esi, 1
3792
        mov     [edx+1], al
3793
        shr     eax, 4
3794
        mov     edx, [edx+4]
9715 Doczom 3795
        mov     eax, [edx + eax*4]
2288 clevermous 3796
        pop     edx
3797
        ret     4
2430 mario79 3798
;-----------------------------------------------------------------------------
3799
align 4
2288 clevermous 3800
putimage_init32bpp:
3801
        shl     eax, 2
3802
        ret
2430 mario79 3803
;-----------------------------------------------------------------------------
2288 clevermous 3804
align 16
3805
putimage_get32bpp:
3806
        lodsd
3807
        ret     4
2430 mario79 3808
;-----------------------------------------------------------------------------
3809
align 4
2288 clevermous 3810
putimage_init15bpp:
3811
putimage_init16bpp:
3812
        add     eax, eax
3813
        ret
2430 mario79 3814
;-----------------------------------------------------------------------------
2288 clevermous 3815
align 16
3816
putimage_get15bpp:
3817
; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
3818
        push    ecx edx
3819
        movzx   eax, word [esi]
3820
        add     esi, 2
3821
        mov     ecx, eax
3822
        mov     edx, eax
3823
        and     eax, 0x1F
3824
        and     ecx, 0x1F shl 5
3825
        and     edx, 0x1F shl 10
3826
        shl     eax, 3
3827
        shl     ecx, 6
3828
        shl     edx, 9
3829
        or      eax, ecx
3830
        or      eax, edx
3831
        pop     edx ecx
3832
        ret     4
2430 mario79 3833
;-----------------------------------------------------------------------------
2288 clevermous 3834
align 16
3835
putimage_get16bpp:
3836
; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
3837
        push    ecx edx
3838
        movzx   eax, word [esi]
3839
        add     esi, 2
3840
        mov     ecx, eax
3841
        mov     edx, eax
3842
        and     eax, 0x1F
3843
        and     ecx, 0x3F shl 5
3844
        and     edx, 0x1F shl 11
3845
        shl     eax, 3
3846
        shl     ecx, 5
3847
        shl     edx, 8
3848
        or      eax, ecx
3849
        or      eax, edx
3850
        pop     edx ecx
3851
        ret     4
2430 mario79 3852
;-----------------------------------------------------------------------------
3853
;align 4
2288 clevermous 3854
; eax x beginning
3855
; ebx y beginning
3856
; ecx x end
3857
        ; edx y end
3858
; edi color
2430 mario79 3859
;__sys_drawbar:
9949 Doczom 3860
;        mov     esi, [current_slot]
3861
;        mov     esi, [esi + APPDATA.window]
3862
;        add     eax, [esi + WDATA.clientbox.left]
3863
;        add     ecx, [esi + WDATA.clientbox.left]
3864
;        add     ebx, [esi + WDATA.clientbox.top]
3865
;        add     edx, [esi + WDATA.clientbox.top]
2430 mario79 3866
;--------------------------------------
3867
;align 4
3868
;.forced:
3869
;        call    vesa20_drawbar
2407 mario79 3870
;        call    [draw_pointer]
3871
;        ret
2430 mario79 3872
;-----------------------------------------------------------------------------
2288 clevermous 3873
if used _rdtsc
3874
_rdtsc:
3875
        bt      [cpu_caps], CAPS_TSC
9910 Doczom 3876
        jnc     .ret_rdtsc
2288 clevermous 3877
        rdtsc
3878
        ret
9910 Doczom 3879
   .ret_rdtsc:
2288 clevermous 3880
        mov     edx, 0xffffffff
3881
        mov     eax, 0xffffffff
3882
        ret
3883
end if
3884
 
3885
sys_msg_board_str:
3886
 
3887
        pushad
3888
   @@:
3889
        cmp     [esi], byte 0
3890
        je      @f
3614 shikhin 3891
        mov     ebx, 1
3892
        movzx   ecx, byte [esi]
2288 clevermous 3893
        call    sys_msg_board
3894
        inc     esi
3895
        jmp     @b
3896
   @@:
3897
        popad
3898
        ret
3899
 
3900
sys_msg_board_byte:
3901
; in: al = byte to display
3902
; out: nothing
3903
; destroys: nothing
3904
        pushad
3905
        mov     ecx, 2
3906
        shl     eax, 24
3907
        jmp     @f
3908
 
3909
sys_msg_board_word:
3910
; in: ax = word to display
3911
; out: nothing
3912
; destroys: nothing
3913
        pushad
3914
        mov     ecx, 4
3915
        shl     eax, 16
3916
        jmp     @f
3917
 
3918
sys_msg_board_dword:
3919
; in: eax = dword to display
3920
; out: nothing
3921
; destroys: nothing
3922
        pushad
3923
        mov     ecx, 8
3924
@@:
3925
        push    ecx
3926
        rol     eax, 4
3927
        push    eax
3928
        and     al, 0xF
3929
        cmp     al, 10
3930
        sbb     al, 69h
3931
        das
3614 shikhin 3932
        mov     cl, al
3933
        xor     ebx, ebx
3934
        inc     ebx
2288 clevermous 3935
        call    sys_msg_board
3936
        pop     eax
3937
        pop     ecx
3938
        loop    @b
3939
        popad
3940
        ret
3941
 
3392 clevermous 3942
msg_board_data_size = 65536 ; Must be power of two
3943
 
2288 clevermous 3944
uglobal
6501 pathoswith 3945
msg_board_data  rb  msg_board_data_size
3946
msg_board_count dd  ?
2288 clevermous 3947
endg
3948
 
6501 pathoswith 3949
iglobal
3950
msg_board_pos   dd  42*6*65536+10 ; for printing debug output on the screen
3951
endg
3952
 
2288 clevermous 3953
sys_msg_board:
6501 pathoswith 3954
; ebx=1 -> write, cl = byte to write
3955
; ebx=2 -> read, ecx=0 -> no data, ecx=1 -> data in al
3956
        push    eax ebx
3614 shikhin 3957
        mov     eax, ebx
3958
        mov     ebx, ecx
2288 clevermous 3959
        mov     ecx, [msg_board_count]
3960
        cmp     eax, 1
6501 pathoswith 3961
        jne     .read
2288 clevermous 3962
 
3963
if defined debug_com_base
3964
        push    dx ax
6501 pathoswith 3965
@@: ; wait for empty transmit register
2288 clevermous 3966
        mov     dx, debug_com_base+5
3967
        in      al, dx
3968
        test    al, 1 shl 5
3969
        jz      @r
3970
        mov     dx, debug_com_base      ; Output the byte
3971
        mov     al, bl
3972
        out     dx, al
3973
        pop     ax dx
3974
end if
3975
 
3976
        mov     [msg_board_data+ecx], bl
3777 yogev_ezra 3977
        cmp     byte [debug_direct_print], 1
6501 pathoswith 3978
        jnz     .end
3507 clevermous 3979
        pusha
3980
        lea     edx, [msg_board_data+ecx]
3981
        mov     ecx, 0x40FFFFFF
3982
        mov     ebx, [msg_board_pos]
3983
        mov     edi, 1
9268 Doczom 3984
        mov     esi, edi ;1
3507 clevermous 3985
        call    dtext
3986
        popa
3987
        add     word [msg_board_pos+2], 6
6501 pathoswith 3988
        cmp     word [msg_board_pos+2], 105*6
3989
        jnc     @f
3507 clevermous 3990
        cmp     bl, 10
6501 pathoswith 3991
        jnz     .end
3992
@@:
3993
        mov     word [msg_board_pos+2], 42*6
3507 clevermous 3994
        add     word [msg_board_pos], 10
6501 pathoswith 3995
        mov     eax, [_display.height]
3996
        sub     eax, 10
3997
        cmp     ax, word [msg_board_pos]
3998
        jnc     @f
3507 clevermous 3999
        mov     word [msg_board_pos], 10
4000
@@:
3534 clevermous 4001
        pusha
6501 pathoswith 4002
        mov     eax, [msg_board_pos]
4003
        movzx   ebx, ax
4004
        shr     eax, 16
4005
        mov     edx, 105*6
4006
        xor     ecx, ecx
4007
        mov     edi, 1
4008
        mov     esi, 9
4009
@@:
4010
        call    hline
4011
        inc     ebx
4012
        dec     esi
4013
        jnz     @b
3534 clevermous 4014
        popa
6501 pathoswith 4015
.end:
2288 clevermous 4016
        inc     ecx
3392 clevermous 4017
        and     ecx, msg_board_data_size - 1
2288 clevermous 4018
        mov     [msg_board_count], ecx
6501 pathoswith 4019
.ret:
3614 shikhin 4020
        pop     ebx eax
2288 clevermous 4021
        ret
6501 pathoswith 4022
 
4023
.read:
2288 clevermous 4024
        cmp     eax, 2
6501 pathoswith 4025
        jne     .ret
9897 dunkaist 4026
        add     esp, 8  ; returning data in ebx and eax, so no need to restore them
2288 clevermous 4027
        test    ecx, ecx
9897 dunkaist 4028
        jnz     @f
4029
        mov     [esp + SYSCALL_STACK.eax], ecx
4030
        mov     [esp + SYSCALL_STACK.ebx], ecx
4031
        ret
4032
@@:
2288 clevermous 4033
        mov     eax, msg_board_data+1
4034
        mov     ebx, msg_board_data
4035
        movzx   edx, byte [ebx]
4036
        call    memmove
4037
        dec     [msg_board_count]
9897 dunkaist 4038
        mov     [esp + SYSCALL_STACK.eax], edx
4039
        mov     [esp + SYSCALL_STACK.ebx], 1
2288 clevermous 4040
        ret
4041
 
4042
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4043
;; 61 sys function.                                                ;;
4044
;; in eax=61,ebx in [1..3]                                         ;;
4045
;; out eax                                                         ;;
4046
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4047
iglobal
4048
align 4
4049
f61call:
4050
           dd sys_gs.1   ; resolution
4051
           dd sys_gs.2   ; bits per pixel
4052
           dd sys_gs.3   ; bytes per scanline
4053
endg
4054
 
4055
 
4056
align 4
4057
 
4058
sys_gs:                         ; direct screen access
4059
        dec     ebx
4060
        cmp     ebx, 2
4061
        ja      .not_support
9715 Doczom 4062
        jmp     dword [f61call + ebx*4]
2288 clevermous 4063
.not_support:
9831 dunkaist 4064
        or      [esp + SYSCALL_STACK.eax], dword -1
2288 clevermous 4065
        ret
4066
 
4067
 
4068
.1:                             ; resolution
5350 serge 4069
        mov     eax, [_display.width]
2288 clevermous 4070
        shl     eax, 16
5350 serge 4071
        mov     ax, word [_display.height]
9831 dunkaist 4072
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 4073
        ret
4074
.2:                             ; bits per pixel
5154 hidnplayr 4075
        mov     eax, [_display.bits_per_pixel]
9831 dunkaist 4076
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 4077
        ret
4078
.3:                             ; bytes per scanline
5351 serge 4079
        mov     eax, [_display.lfb_pitch]
9831 dunkaist 4080
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 4081
        ret
4082
 
4083
align 4
4084
syscall_getscreensize:                  ; GetScreenSize
5350 serge 4085
        mov     ax, word [_display.width]
4086
        dec     ax
2288 clevermous 4087
        shl     eax, 16
5350 serge 4088
        mov     ax, word [_display.height]
4089
        dec     ax
9831 dunkaist 4090
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 4091
        ret
4700 mario79 4092
;-----------------------------------------------------------------------------
2288 clevermous 4093
align 4
4713 mario79 4094
syscall_cdaudio:
4711 mario79 4095
; ECX - position of CD/DVD-drive
4096
; from 0=Primary Master to 3=Secondary Slave for first IDE contr.
4097
; from 4=Primary Master to 7=Secondary Slave for second IDE contr.
4098
; from 8=Primary Master to 11=Secondary Slave for third IDE contr.
4099
        cmp     ecx, 11
4100
        ja      .exit
4101
 
4102
        mov     eax, ecx
4103
        shr     eax, 2
4104
        lea     eax, [eax*5]
9715 Doczom 4105
        mov     al, [eax + DRIVE_DATA + 1]
4711 mario79 4106
 
4107
        push    ecx ebx
4108
        mov     ebx, ecx
4109
        and     ebx, 11b
4110
        shl     ebx, 1
4111
        mov     cl, 6
4112
        sub     cl, bl
4113
        shr     al, cl
4114
        test    al, 2 ; it's not an ATAPI device
4115
        pop     ebx ecx
4116
 
4117
        jz      .exit
4118
 
4700 mario79 4119
        cmp     ebx, 4
4120
        je      .eject
2288 clevermous 4121
 
4122
        cmp     ebx, 5
4700 mario79 4123
        je      .load
4711 mario79 4124
;--------------------------------------
4125
.exit:
4700 mario79 4126
        ret
4127
;--------------------------------------
2288 clevermous 4128
.load:
4129
        call    .reserve
4130
        call    LoadMedium
4131
        jmp     .free
4700 mario79 4132
;--------------------------------------
2288 clevermous 4133
.eject:
4134
        call    .reserve
4135
        call    clear_CD_cache
4136
        call    allow_medium_removal
4137
        call    EjectMedium
4138
        jmp     .free
4700 mario79 4139
;--------------------------------------
2288 clevermous 4140
.reserve:
4141
        call    reserve_cd
4700 mario79 4142
 
4143
        mov     ebx, ecx
4144
        inc     ebx
4145
        mov     [cdpos], ebx
4146
 
2288 clevermous 4147
        mov     eax, ecx
4148
        shr     eax, 1
4149
        and     eax, 1
4150
        inc     eax
6464 pathoswith 4151
        mov     [ChannelNumber], al
2288 clevermous 4152
        mov     eax, ecx
4153
        and     eax, 1
4154
        mov     [DiskNumber], al
4155
        call    reserve_cd_channel
4156
        ret
4700 mario79 4157
;--------------------------------------
2288 clevermous 4158
.free:
4159
        call    free_cd_channel
4160
        and     [cd_status], 0
4161
        ret
2511 mario79 4162
;-----------------------------------------------------------------------------
2288 clevermous 4163
align 4
2511 mario79 4164
syscall_getpixel_WinMap:                       ; GetPixel WinMap
9948 Doczom 4165
        xor     eax, eax
4166
 
5350 serge 4167
        cmp     ebx, [_display.width]
9948 Doczom 4168
        jae     .store
5350 serge 4169
        cmp     ecx, [_display.height]
9948 Doczom 4170
        jae     .store
2511 mario79 4171
;--------------------------------------
4172
        mov     eax, [d_width_calc_area + ecx*4]
5351 serge 4173
        add     eax, [_display.win_map]
2511 mario79 4174
        movzx   eax, byte[eax+ebx]        ; get value for current point
4175
;--------------------------------------
4176
align 4
4177
.store:
9831 dunkaist 4178
        mov     [esp + SYSCALL_STACK.eax], eax
2511 mario79 4179
        ret
4180
;-----------------------------------------------------------------------------
4181
align 4
2288 clevermous 4182
syscall_getpixel:                       ; GetPixel
5350 serge 4183
        mov     ecx, [_display.width]
2288 clevermous 4184
        xor     edx, edx
4185
        mov     eax, ebx
4186
        div     ecx
4187
        mov     ebx, edx
4188
        xchg    eax, ebx
2430 mario79 4189
        and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
2288 clevermous 4190
        call    dword [GETPIXEL]; eax - x, ebx - y
9831 dunkaist 4191
        mov     [esp + SYSCALL_STACK.eax], ecx
2288 clevermous 4192
        ret
2509 mario79 4193
;-----------------------------------------------------------------------------
2288 clevermous 4194
align 4
4195
syscall_getarea:
4196
;eax = 36
4197
;ebx = pointer to bufer for img BBGGRRBBGGRR...
4198
;ecx = [size x]*65536 + [size y]
4199
;edx = [start x]*65536 + [start y]
4200
        pushad
4201
        mov     edi, ebx
4202
        mov     eax, edx
4203
        shr     eax, 16
4204
        mov     ebx, edx
4205
        and     ebx, 0xffff
4206
        dec     eax
4207
        dec     ebx
4208
     ; eax - x, ebx - y
4209
        mov     edx, ecx
4210
 
4211
        shr     ecx, 16
4212
        and     edx, 0xffff
4213
        mov     esi, ecx
4214
     ; ecx - size x, edx - size y
4215
 
4216
        mov     ebp, edx
8598 rgimad 4217
        lea     ebp, [ebp*3]
4218
        imul    ebp, esi
4219
        stdcall is_region_userspace, edi, ebp
9045 dunkaist 4220
        jnz     .exit
8598 rgimad 4221
 
4222
        mov     ebp, edx
2288 clevermous 4223
        dec     ebp
4224
        lea     ebp, [ebp*3]
4225
 
4226
        imul    ebp, esi
4227
 
4228
        mov     esi, ecx
4229
        dec     esi
4230
        lea     esi, [esi*3]
4231
 
4232
        add     ebp, esi
4233
        add     ebp, edi
4234
 
4235
        add     ebx, edx
2509 mario79 4236
;--------------------------------------
4237
align 4
2288 clevermous 4238
.start_y:
4239
        push    ecx edx
2509 mario79 4240
;--------------------------------------
4241
align 4
2288 clevermous 4242
.start_x:
4243
        push    eax ebx ecx
4244
        add     eax, ecx
4245
 
2430 mario79 4246
        and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
2288 clevermous 4247
        call    dword [GETPIXEL]; eax - x, ebx - y
4248
 
4249
        mov     [ebp], cx
4250
        shr     ecx, 16
4251
        mov     [ebp+2], cl
4252
 
4253
        pop     ecx ebx eax
4254
        sub     ebp, 3
4255
        dec     ecx
4256
        jnz     .start_x
4257
        pop     edx ecx
4258
        dec     ebx
4259
        dec     edx
4260
        jnz     .start_y
9947 ace_dent 4261
 
8598 rgimad 4262
.exit:
2288 clevermous 4263
        popad
4264
        ret
2509 mario79 4265
;-----------------------------------------------------------------------------
8675 rgimad 4266
 
2288 clevermous 4267
align 4
4268
syscall_threads:                        ; CreateThreads
4269
;
4105 Serge 4270
;   ecx=thread entry point
4271
;   edx=thread stack pointer
2288 clevermous 4272
;
4273
; on return : eax = pid
4105 Serge 4274
        xor     ebx, ebx
2288 clevermous 4275
        call    new_sys_threads
4276
 
9831 dunkaist 4277
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 4278
        ret
4279
 
2438 mario79 4280
;------------------------------------------------------------------------------
2288 clevermous 4281
align 4
2438 mario79 4282
calculate_fast_getting_offset_for_WinMapAddress:
4283
; calculate data area for fast getting offset to _WinMapAddress
2446 mario79 4284
        xor     eax, eax
2438 mario79 4285
        mov     ecx, [_display.height]
4286
        mov     edi, d_width_calc_area
4287
        cld
4288
@@:
4289
        stosd
4290
        add     eax, [_display.width]
4291
        dec     ecx
4292
        jnz     @r
4293
        ret
4294
;------------------------------------------------------------------------------
4295
align 4
2480 mario79 4296
calculate_fast_getting_offset_for_LFB:
4297
; calculate data area for fast getting offset to LFB
4298
        xor     eax, eax
4299
        mov     ecx, [_display.height]
4300
        mov     edi, BPSLine_calc_area
4301
        cld
4302
@@:
4303
        stosd
5351 serge 4304
        add     eax, [_display.lfb_pitch]
2480 mario79 4305
        dec     ecx
4306
        jnz     @r
4307
        ret
4308
;------------------------------------------------------------------------------
4309
align 4
2288 clevermous 4310
set_screen:
2654 mario79 4311
; in:
4312
; eax - new Screen_Max_X
4313
; ecx - new BytesPerScanLine
4314
; edx - new Screen_Max_Y
2288 clevermous 4315
 
4316
        pushfd
4317
        cli
4318
 
5351 serge 4319
        mov     [_display.lfb_pitch], ecx
2288 clevermous 4320
 
4321
        mov     [screen_workarea.right], eax
4322
        mov     [screen_workarea.bottom], edx
4323
 
4324
        push    ebx
4325
        push    esi
4326
        push    edi
4327
 
4328
        pushad
4329
 
2654 mario79 4330
        cmp     [do_not_touch_winmap], 1
4331
        je      @f
4332
 
5351 serge 4333
        stdcall kernel_free, [_display.win_map]
2288 clevermous 4334
 
4335
        mov     eax, [_display.width]
4336
        mul     [_display.height]
5351 serge 4337
        mov     [_display.win_map_size], eax
2288 clevermous 4338
 
4339
        stdcall kernel_alloc, eax
5351 serge 4340
        mov     [_display.win_map], eax
2288 clevermous 4341
        test    eax, eax
4342
        jz      .epic_fail
2654 mario79 4343
; store for f.18.24
4344
        mov     eax, [_display.width]
4345
        mov     [display_width_standard], eax
2288 clevermous 4346
 
2654 mario79 4347
        mov     eax, [_display.height]
4348
        mov     [display_height_standard], eax
4349
@@:
2438 mario79 4350
        call    calculate_fast_getting_offset_for_WinMapAddress
2545 mario79 4351
; for Qemu or non standart video cards
2991 Serge 4352
; Unfortunately [BytesPerScanLine] does not always
2545 mario79 4353
;                             equal to [_display.width] * [ScreenBPP] / 8
4354
        call    calculate_fast_getting_offset_for_LFB
2288 clevermous 4355
        popad
4356
 
4357
        call    repos_windows
4358
        xor     eax, eax
4359
        xor     ebx, ebx
5350 serge 4360
        mov     ecx, [_display.width]
4361
        mov     edx, [_display.height]
4362
        dec     ecx
4363
        dec     edx
2288 clevermous 4364
        call    calculatescreen
4365
        pop     edi
4366
        pop     esi
4367
        pop     ebx
4368
 
4369
        popfd
4370
        ret
4371
 
4372
.epic_fail:
4373
        hlt                     ; Houston, we've had a problem
4374
 
4375
; --------------- APM ---------------------
4376
uglobal
4377
apm_entry       dp      0
4378
apm_vf          dd      0
4379
endg
4380
 
4381
align 4
4382
sys_apm:
4383
        xor     eax, eax
4384
        cmp     word [apm_vf], ax       ; Check APM BIOS enable
4385
        jne     @f
4386
        inc     eax
4387
        or      dword [esp + 44], eax   ; error
4388
        add     eax, 7
9911 Doczom 4389
        mov     dword [esp + SYSCALL_STACK.eax], eax   ; 32-bit protected-mode interface not supported
2288 clevermous 4390
        ret
4391
 
4392
@@:
4393
;       xchg    eax, ecx
4394
;       xchg    ebx, ecx
4395
 
4396
        cmp     dx, 3
4397
        ja      @f
4398
        and     [esp + 44], byte 0xfe    ; emulate func 0..3 as func 0
4399
        mov     eax, [apm_vf]
9911 Doczom 4400
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 4401
        shr     eax, 16
9911 Doczom 4402
        mov     [esp + SYSCALL_STACK.ecx], eax
2288 clevermous 4403
        ret
4404
 
4405
@@:
4406
 
4407
        mov     esi, [master_tab+(OS_BASE shr 20)]
4408
        xchg    [master_tab], esi
4409
        push    esi
4410
        mov     edi, cr3
4411
        mov     cr3, edi                ;flush TLB
4412
 
4413
        call    pword [apm_entry]       ;call APM BIOS
4414
 
4415
        xchg    eax, [esp]
4416
        mov     [master_tab], eax
4417
        mov     eax, cr3
4418
        mov     cr3, eax
4419
        pop     eax
4420
 
9911 Doczom 4421
        mov     [esp + SYSCALL_STACK.edi], edi
4422
        mov     [esp + SYSCALL_STACK.esi], esi
4423
        mov     [esp + SYSCALL_STACK.ebx], ebx
4424
        mov     [esp + SYSCALL_STACK.edx], edx
4425
        mov     [esp + SYSCALL_STACK.ecx], ecx
4426
        mov     [esp + SYSCALL_STACK.eax], eax
2288 clevermous 4427
        setc    al
4428
        and     [esp + 44], byte 0xfe
4429
        or      [esp + 44], al
4430
        ret
4431
; -----------------------------------------
4432
 
4433
align 4
4434
undefined_syscall:                      ; Undefined system call
9831 dunkaist 4435
        mov     [esp + SYSCALL_STACK.eax], dword -1
2288 clevermous 4436
        ret
4437
 
8160 rgimad 4438
align 4
8962 Boppan 4439
; @brief Check if given memory region lays in lower 2gb (userspace memory) or not
4440
; @param base Base address of region
4441
; @param len Lenght of region
4442
; @return ZF = 1 if region in userspace memory,
4443
;         ZF = 0 otherwise
8329 rgimad 4444
proc is_region_userspace stdcall, base:dword, len:dword
9045 dunkaist 4445
        push    eax
8329 rgimad 4446
        mov     eax, [base]
8216 rgimad 4447
 
9045 dunkaist 4448
        cmp     eax, OS_BASE-1
4449
        ja      @f              ; zf
8216 rgimad 4450
 
8329 rgimad 4451
        add     eax, [len]
9045 dunkaist 4452
        jc      @f              ; zf
8160 rgimad 4453
        cmp     eax, OS_BASE
9045 dunkaist 4454
        ja      @f              ; zf
2288 clevermous 4455
 
9045 dunkaist 4456
        cmp     eax, eax        ; ZF
8160 rgimad 4457
@@:
9045 dunkaist 4458
        pop     eax
9947 ace_dent 4459
        ret
8329 rgimad 4460
endp
8160 rgimad 4461
 
9823 dunkaist 4462
align 4
4463
; @brief Check whether given string lays in userspace memory, i.e. below OS_BASE
4464
; @param base Base address of string
4465
; @return ZF = 1 if string in userspace memory,
4466
;         zf = 0 otherwise
4467
proc is_string_userspace stdcall, base:dword
4468
        push    eax ecx edi
4469
        xor     eax, eax
4470
        mov     edi, [base]
4471
 
4472
        mov     ecx, OS_BASE-1
4473
        sub     ecx, edi
4474
        jb      .done           ; zf
4475
        inc     ecx
4476
        cmp     ecx, 0x10000    ; don't allow strings larger than 64k?
4477
        jbe     @f
4478
        mov     ecx, 0x10000
4479
@@:
4480
        repnz scasb
4481
.done:
4482
        pop     edi ecx eax
4483
        ret
4484
endp
4485
 
3627 Serge 4486
if ~ lang eq sp
4487
diff16 "end of .text segment",0,$
4488
end if
4489
 
2288 clevermous 4490
include "data32.inc"
4491
 
4492
__REV__ = __REV
4493
 
3341 yogev_ezra 4494
if ~ lang eq sp
2288 clevermous 4495
diff16 "end of kernel code",0,$
3287 esevece 4496
end if