Skip to content

Commit

Permalink
hal/ia32: cast operands to correct sizes
Browse files Browse the repository at this point in the history
Copy "in" related function from efi.
Newer binutils versions check closely if asm instructions get operands
of correct size.

JIRA: RTOS-912
  • Loading branch information
badochov committed Sep 23, 2024
1 parent cfd2a03 commit 41d9dfb
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 135 deletions.
18 changes: 9 additions & 9 deletions devices/tty-bios/tty-bios.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static const unsigned char ansi2bg[] = {

typedef struct {
volatile u16 *vram; /* Video memory */
void *crtc; /* CRT controller register */
u16 crtc; /* CRT controller register */
unsigned int rows; /* Terminal height */
unsigned int cols; /* Terminal width */
unsigned char attr; /* Character attribute */
Expand Down Expand Up @@ -228,9 +228,9 @@ static ssize_t ttybios_write(unsigned int minor, addr_t offs, const void *buff,

/* Print from current cursor position */
hal_outb(tty->crtc, 0x0f);
pos = hal_inb((void *)((addr_t)tty->crtc + 1));
pos = hal_inb(tty->crtc + 1);
hal_outb(tty->crtc, 0x0e);
pos |= (u16)hal_inb((void *)((addr_t)tty->crtc + 1)) << 8;
pos |= (u16)hal_inb(tty->crtc + 1) << 8;
row = pos / tty->cols;
col = pos % tty->cols;

Expand Down Expand Up @@ -414,7 +414,7 @@ static ssize_t ttybios_write(unsigned int minor, addr_t offs, const void *buff,
switch (tty->parms[0]) {
case 25:
hal_outb(tty->crtc, 0x0a);
hal_outb((void *)((addr_t)tty->crtc + 1), hal_inb((void *)((addr_t)tty->crtc + 1)) & ~0x20);
hal_outb(tty->crtc + 1, hal_inb(tty->crtc + 1) & ~0x20);
break;
}
tty->esc = esc_init;
Expand All @@ -424,7 +424,7 @@ static ssize_t ttybios_write(unsigned int minor, addr_t offs, const void *buff,
switch (tty->parms[0]) {
case 25:
hal_outb(tty->crtc, 0x0a);
hal_outb((void *)((addr_t)tty->crtc + 1), hal_inb((void *)((addr_t)tty->crtc + 1)) | 0x20);
hal_outb(tty->crtc + 1, hal_inb(tty->crtc + 1) | 0x20);
break;
}
tty->esc = esc_init;
Expand Down Expand Up @@ -456,9 +456,9 @@ static ssize_t ttybios_write(unsigned int minor, addr_t offs, const void *buff,
/* Update cursor */
i = row * tty->cols + col;
hal_outb(tty->crtc, 0x0e);
hal_outb((void *)((addr_t)tty->crtc + 1), i >> 8);
hal_outb(tty->crtc + 1, i >> 8);
hal_outb(tty->crtc, 0x0f);
hal_outb((void *)((addr_t)tty->crtc + 1), i);
hal_outb(tty->crtc + 1, i);
*((u8 *)(tty->vram + i) + 1) = tty->attr;
}

Expand Down Expand Up @@ -507,11 +507,11 @@ static int ttybios_init(unsigned int minor)
return -EINVAL;

/* Check color support */
color = hal_inb((void *)0x3cc) & 0x01;
color = hal_inb((u16)0x3cc) & 0x01;

/* Initialize VGA */
tty->vram = (u16 *)(color ? 0xb8000 : 0xb0000);
tty->crtc = (void *)(color ? 0x3d4 : 0x3b4);
tty->crtc = (u16)(color ? 0x3d4 : 0x3b4);

/* Default 80x25 text mode with magenta color attribute */
tty->rows = 25;
Expand Down
16 changes: 8 additions & 8 deletions devices/uart-16550/uarthw-pc.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ unsigned char uarthw_read(void *hwctx, unsigned int reg)

/* Read from IO-port */
if (addr & 0x1)
return hal_inb((void *)((addr & ~0x3) + reg));
return hal_inb((u16)((addr & ~0x3) + reg));

/* Read from memory */
return *(ctx->base + reg);
Expand All @@ -53,7 +53,7 @@ void uarthw_write(void *hwctx, unsigned int reg, unsigned char val)

/* Write to IO-port */
if (addr & 0x1) {
hal_outb((void *)((addr & ~0x3) + reg), val);
hal_outb((u16)((addr & ~0x3) + reg), val);
return;
}

Expand All @@ -65,21 +65,21 @@ void uarthw_write(void *hwctx, unsigned int reg, unsigned char val)
/* SCH311X Super IO controller */


static unsigned char uarthw_SCH311Xinb(void *base, unsigned int reg)
static unsigned char uarthw_SCH311Xinb(u16 base, unsigned int reg)
{
hal_outb(base, reg);
return hal_inb(base + 1);
}


static void uarthw_SCH311Xoutb(void *base, unsigned int reg, unsigned char val)
static void uarthw_SCH311Xoutb(u16 base, unsigned int reg, unsigned char val)
{
hal_outb(base, reg);
hal_outb(base + 1, val);
}


static int uarthw_SCH311Xdetect(void *base)
static int uarthw_SCH311Xdetect(u16 base)
{
int ret;

Expand Down Expand Up @@ -107,7 +107,7 @@ static int uarthw_SCH311Xdetect(void *base)
}


static int uarthw_SCH311Xmode(void *base, unsigned char n, unsigned char mode)
static int uarthw_SCH311Xmode(u16 base, unsigned char n, unsigned char mode)
{
/* Enter configuration mode */
hal_outb(base, 0x55);
Expand Down Expand Up @@ -141,9 +141,9 @@ int uarthw_init(unsigned int n, void *hwctx, unsigned int *baud)
/* Set preferred baudrate */
if (baud != NULL) {
/* SCH311X Super IO controller has at least 2 high speed UARTS */
if ((n < 2) && (uarthw_SCH311Xdetect((void *)0x2e) >= 0)) {
if ((n < 2) && (uarthw_SCH311Xdetect((u16)0x2e) >= 0)) {
*baud = bps_460800;
uarthw_SCH311Xmode((void *)0x2e, 0x04 + n, bauds[*baud].mode);
uarthw_SCH311Xmode((u16)0x2e, 0x04 + n, bauds[*baud].mode);
}
else {
*baud = bps_115200;
Expand Down
4 changes: 2 additions & 2 deletions hal/ia32/console-serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static unsigned char console_read(unsigned int reg)

/* Read from IO-port */
if (addr & 0x1)
return hal_inb((void *)((addr & ~0x3) + reg));
return hal_inb((u16)((addr & ~0x3) + reg));

/* Read from memory */
return *(halconsole_common.base + reg);
Expand All @@ -58,7 +58,7 @@ static void console_write(unsigned int reg, unsigned char val)

/* Write to IO-port */
if (addr & 0x1) {
hal_outb((void *)((addr & ~0x3) + reg), val);
hal_outb((u16)((addr & ~0x3) + reg), val);
return;
}

Expand Down
16 changes: 8 additions & 8 deletions hal/ia32/console-vga.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static const unsigned char ansi2bg[] = {

struct {
volatile u16 *vram; /* Video memory */
void *crtc; /* CRT controller register */
u16 crtc; /* CRT controller register */
unsigned int rows; /* Console height */
unsigned int cols; /* Console width */
unsigned char attr; /* Character attribute */
Expand Down Expand Up @@ -91,9 +91,9 @@ void hal_consolePrint(const char *s)

/* Print from current cursor position */
hal_outb(halconsole_common.crtc, 0x0f);
pos = hal_inb((void *)((addr_t)halconsole_common.crtc + 1));
pos = hal_inb(halconsole_common.crtc + 1);
hal_outb(halconsole_common.crtc, 0x0e);
pos |= (u16)hal_inb((void *)((addr_t)halconsole_common.crtc + 1)) << 8;
pos |= (u16)hal_inb(halconsole_common.crtc + 1) << 8;
row = pos / halconsole_common.cols;
col = pos % halconsole_common.cols;

Expand Down Expand Up @@ -277,7 +277,7 @@ void hal_consolePrint(const char *s)
switch (halconsole_common.parms[0]) {
case 25:
hal_outb(halconsole_common.crtc, 0x0a);
hal_outb((void *)((addr_t)halconsole_common.crtc + 1), hal_inb((void *)((addr_t)halconsole_common.crtc + 1)) & ~0x20);
hal_outb(halconsole_common.crtc + 1, hal_inb(halconsole_common.crtc + 1) & ~0x20);
break;
}
halconsole_common.esc = esc_init;
Expand All @@ -287,7 +287,7 @@ void hal_consolePrint(const char *s)
switch (halconsole_common.parms[0]) {
case 25:
hal_outb(halconsole_common.crtc, 0x0a);
hal_outb((void *)((addr_t)halconsole_common.crtc + 1), hal_inb((void *)((addr_t)halconsole_common.crtc + 1)) | 0x20);
hal_outb(halconsole_common.crtc + 1, hal_inb(halconsole_common.crtc + 1) | 0x20);
break;
}
halconsole_common.esc = esc_init;
Expand Down Expand Up @@ -319,9 +319,9 @@ void hal_consolePrint(const char *s)
/* Update cursor */
i = row * halconsole_common.cols + col;
hal_outb(halconsole_common.crtc, 0x0e);
hal_outb((void *)((addr_t)halconsole_common.crtc + 1), i >> 8);
hal_outb(halconsole_common.crtc + 1, i >> 8);
hal_outb(halconsole_common.crtc, 0x0f);
hal_outb((void *)((addr_t)halconsole_common.crtc + 1), i);
hal_outb(halconsole_common.crtc + 1, i);
*((u8 *)(halconsole_common.vram + i) + 1) = halconsole_common.attr;
}

Expand All @@ -340,7 +340,7 @@ void hal_consoleInit(void)

/* Initialize VGA */
halconsole_common.vram = (u16 *)(color ? 0xb8000 : 0xb0000);
halconsole_common.crtc = (void *)(color ? 0x3d4 : 0x3b4);
halconsole_common.crtc = (u16)(color ? 0x3d4 : 0x3b4);

/* Default 80x25 text mode with magenta color attribute */
halconsole_common.rows = 25;
Expand Down
8 changes: 4 additions & 4 deletions hal/ia32/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,18 @@ void hal_cpuReboot(void)

/* 1. Try to reboot using keyboard controller (8042) */
for (timeout = 0xffff; timeout != 0; --timeout) {
status = hal_inb((void *)0x64);
status = hal_inb((u16)0x64);
if ((status & 1) != 0) {
(void)hal_inb((void *)0x60);
(void)hal_inb((u16)0x60);
}
if ((status & 2) == 0) {
break;
}
}
hal_outb((void *)0x64, 0xfe);
hal_outb((u16)0x64, 0xfe);

/* 2. Try to reboot by PCI reset */
hal_outb((void *)0xcf9, 0xe);
hal_outb((u16)0xcf9, 0xe);

/* 3. Triple fault (interrupt with null idt) */
__asm__ volatile(
Expand Down
99 changes: 64 additions & 35 deletions hal/ia32/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#define _CPU_H_


#include "types.h"


static inline void hal_cpuDataMemoryBarrier(void)
{
/* not supported */
Expand All @@ -36,66 +39,92 @@ static inline void hal_cpuInstrBarrier(void)
}


static inline unsigned char hal_inb(void *addr)
static inline u8 hal_inb(u16 addr)
{
unsigned char val;

__asm__ volatile(
"inb %1, %0; "
: "=a" (val)
: "Nd" (addr));

return val;
u8 b;

/* clang-format off */
__asm__ volatile (
"inb %1, %0\n\t"
: "=a" (b)
: "d" (addr)
: );
/* clang-format on */
return b;
}


static inline void hal_outb(void *addr, unsigned char val)
static inline void hal_outb(u16 addr, u8 b)
{
__asm__ volatile(
"outb %0, %1; "
:: "a" (val), "Nd" (addr));
/* clang-format off */
__asm__ volatile (
"outb %1, %0"
:
: "d" (addr), "a" (b)
: );
/* clang-format on */

return;
}


static inline unsigned short hal_inw(void *addr)
static inline u16 hal_inw(u16 addr)
{
unsigned short val;
u16 w;

__asm__ volatile(
"inw %1, %0; "
: "=a" (val)
: "Nd" (addr));
/* clang-format off */
__asm__ volatile (
"inw %1, %0\n\t"
: "=a" (w)
: "d" (addr)
: );
/* clang-format on */

return val;
return w;
}


static inline void hal_outw(void *addr, unsigned short val)
static inline void hal_outw(u16 addr, u16 w)
{
__asm__ volatile(
"outw %0, %1; "
:: "a" (val), "Nd" (addr));
/* clang-format off */
__asm__ volatile (
"outw %1, %0"
:
: "d" (addr), "a" (w)
: );
/* clang-format on */

return;
}


static inline unsigned int hal_inl(void *addr)
static inline u32 hal_inl(u16 addr)
{
unsigned int val;
u32 l;

__asm__ volatile(
"inl %1, %0; "
: "=a" (val)
: "Nd" (addr));
/* clang-format off */
__asm__ volatile (
"inl %1, %0\n\t"
: "=a" (l)
: "d" (addr)
: );
/* clang-format on */

return val;
return l;
}


static inline void hal_outl(void *addr, unsigned int val)
static inline void hal_outl(u16 addr, u32 l)
{
__asm__ volatile(
"outl %0, %1; "
:: "a" (val), "Nd" (addr));
/* clang-format off */
__asm__ volatile (
"outl %1, %0"
:
: "d" (addr), "a" (l)
: );
/* clang-format on */

return;
}


Expand Down
Loading

0 comments on commit 41d9dfb

Please sign in to comment.