1/*
2 * Copyright 2020, Data61, CSIRO (ABN 41 687 119 230)
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 */
6
7#include <machine/assembler.h>
8
9/*
10 * Calling convention for x86-64
11 * For integer type, the next available register of the sequence %rdi,
12 * %rsi, %rdx, %rcx, %r8 and %r9 are used for passing values. %rax and
13 * %rdx are used for returning integer values. Pointers are considered
14 * as integer type according to the ABI.
15 */
16
17.section .boot.text
18
19.section .text
20
21BEGIN_FUNC(out8)                        # port, value
22    movq    %rdi, %rdx
23    movq    %rsi, %rax
24    outb    %al, %dx
25    ret
26END_FUNC(out8)
27
28BEGIN_FUNC(out16)
29    movq    %rdi, %rdx
30    movq    %rsi, %rax
31    outw    %ax, %dx
32    ret
33END_FUNC(out16)
34
35BEGIN_FUNC(out32)
36    movq    %rdi, %rdx
37    movq    %rsi, %rax
38    outl    %eax, %dx
39    ret
40END_FUNC(out32)
41
42BEGIN_FUNC(in8)
43    movq    $0, %rax
44    movq    %rdi, %rdx
45    inb     %dx, %al
46    ret
47END_FUNC(in8)
48
49BEGIN_FUNC(in16)
50    movq    $0, %rax
51    movq    %rdi, %rdx
52    inw     %dx, %ax
53    ret
54END_FUNC(in16)
55
56BEGIN_FUNC(in32)
57    movq    $0, %rax
58    movq    %rdi, %rdx
59    inl     %dx, %eax
60    ret
61END_FUNC(in32)
62
63BEGIN_FUNC(getCacheLineSize)
64    push    %rbx
65    movl    $1, %eax
66    cpuid
67    movl    %ebx, %eax
68    shrl    $8, %eax
69    andl    $0xff, %eax
70    shll    $3, %eax
71    pop     %rbx
72    ret
73END_FUNC(getCacheLineSize)
74
75BEGIN_FUNC(x64_install_gdt)
76    lgdt    (%rdi)          # load gdtr with gdt pointer
77    movw    $0x10, %ax      # load register ax with seg selector
78    movw    %ax, %ds
79    movw    %ax, %es
80    movw    %ax, %ss
81    movw    $0x0, %ax
82    movw    %ax, %fs
83    movw    %ax, %gs
84    ret
85END_FUNC(x64_install_gdt)
86
87BEGIN_FUNC(x64_install_idt)
88    lidt    (%rdi)
89    ret
90END_FUNC(x64_install_idt)
91
92BEGIN_FUNC(x64_install_ldt)
93    lldt    %di
94    ret
95END_FUNC(x64_install_ldt)
96
97BEGIN_FUNC(x64_install_tss)
98    ltr     %di
99    ret
100END_FUNC(x64_install_tss)
101