Skip to content

Commit

Permalink
pc-tty: pass vram offs to vram accessors instead of ptrs
Browse files Browse the repository at this point in the history
Since the _ttypc_vga_{set,read,write,move} have the ttypc_vt_t already
passed, they can retrieve the vga pointer from that struct. This also
makes the API more secure in case of fbcon, which implicitly assumed
vga == vt->vram when calculating char positions. Now this assumption is
guaranteed

JIRA: RTOS-925
  • Loading branch information
adamgreloch committed Nov 22, 2024
1 parent 6f81c76 commit b7db794
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 41 deletions.
2 changes: 1 addition & 1 deletion tty/pc-tty/ttypc.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ int main(void)
* in the graphic mode already and the text mode buffer may contain garbage */

/* Set default cursor color */
_ttypc_vga_set(ttypc_common.vt, ttypc_common.vt->vram + ttypc_common.vt->cpos, FG_LIGHTGREY << 8, ttypc_common.vt->rows * ttypc_common.vt->cols - ttypc_common.vt->cpos);
_ttypc_vga_set(ttypc_common.vt, ttypc_common.vt->cpos, FG_LIGHTGREY << 8, ttypc_common.vt->rows * ttypc_common.vt->cols - ttypc_common.vt->cpos);

/* Run pool threads */
if ((err = beginthread(ttypc_poolthr, 1, ttypc_common.pstack, sizeof(ttypc_common.pstack), &ttypc_common)) < 0)
Expand Down
36 changes: 20 additions & 16 deletions tty/pc-tty/ttypc_vga.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ static void _ttypc_vga_setctype(ttypc_t *ttypc, uint8_t from, uint8_t to)
}


ssize_t _ttypc_vga_read(volatile uint16_t *vga, uint16_t *buff, size_t n)
ssize_t _ttypc_vga_read(ttypc_vt_t *vt, size_t offs, uint16_t *buff, size_t n)
{
size_t i;
volatile uint16_t *vga = vt->vram + offs;

for (i = 0; i < n; i++)
*(buff + i) = *(vga + i);
Expand All @@ -72,10 +73,11 @@ ssize_t _ttypc_vga_read(volatile uint16_t *vga, uint16_t *buff, size_t n)
}


ssize_t _ttypc_vga_write(ttypc_vt_t *vt, volatile uint16_t *vga, uint16_t *buff, size_t n)
ssize_t _ttypc_vga_write(ttypc_vt_t *vt, size_t offs, uint16_t *buff, size_t n)
{
size_t i;
int pos, col, row;
volatile uint16_t *vga = vt->vram + offs;

pos = vga - vt->vram;
col = pos % vt->cols;
Expand All @@ -98,10 +100,11 @@ ssize_t _ttypc_vga_write(ttypc_vt_t *vt, volatile uint16_t *vga, uint16_t *buff,
}


volatile uint16_t *_ttypc_vga_set(ttypc_vt_t *vt, volatile uint16_t *vga, uint16_t val, size_t n)
volatile uint16_t *_ttypc_vga_set(ttypc_vt_t *vt, size_t offs, uint16_t val, size_t n)
{
size_t i;
int pos, col, row;
volatile uint16_t *vga = vt->vram + offs;

pos = vga - vt->vram;
col = pos % vt->cols;
Expand All @@ -124,10 +127,11 @@ volatile uint16_t *_ttypc_vga_set(ttypc_vt_t *vt, volatile uint16_t *vga, uint16
}


volatile uint16_t *_ttypc_vga_move(ttypc_vt_t *vt, volatile uint16_t *dvga, volatile uint16_t *svga, size_t n)
volatile uint16_t *_ttypc_vga_move(ttypc_vt_t *vt, size_t doffs, size_t soffs, size_t n)
{
size_t i;
int pos, col, row;
volatile uint16_t *dvga = vt->vram + doffs, *svga = vt->vram + soffs;

if (dvga < svga) {
pos = dvga - vt->vram;
Expand Down Expand Up @@ -182,7 +186,7 @@ void _ttypc_vga_switch(ttypc_vt_t *vt)
return;

/* VGA memory -> VT memory */
_ttypc_vga_read(cvt->vram, cvt->mem, cvt->rows * cvt->cols);
_ttypc_vga_read(cvt, 0, cvt->mem, cvt->rows * cvt->cols);
cvt->vram = cvt->mem;

/* Set active VT, do it before writes to unlock access to the fb */
Expand All @@ -196,7 +200,7 @@ void _ttypc_vga_switch(ttypc_vt_t *vt)
mutexLock(vt->lock);
/* VT memory -> VGA memory */
vt->vram = ttypc->vga;
_ttypc_vga_write(vt, vt->vram, vt->mem, vt->rows * vt->cols);
_ttypc_vga_write(vt, 0, vt->mem, vt->rows * vt->cols);
/* Set cursor position... */
_ttypc_vga_setcursor(vt);
/* ... and visibility */
Expand Down Expand Up @@ -233,12 +237,12 @@ void _ttypc_vga_rollup(ttypc_vt_t *vt, unsigned int n)

/* Update scrollback buffer */
_ttypc_vga_allocscrollback(vt, k);
_ttypc_vga_read(vt->vram + (vt->top + n - k) * vt->cols, vt->scrb + (vt->scrbsz - k) * vt->cols, k * vt->cols);
_ttypc_vga_read(vt, (vt->top + n - k) * vt->cols, vt->scrb + (vt->scrbsz - k) * vt->cols, k * vt->cols);

/* Roll up */
if (n < vt->bottom - vt->top) {
_ttypc_vga_move(vt, vt->vram + vt->top * vt->cols, vt->vram + (vt->top + n) * vt->cols, (vt->bottom - vt->top + 1 - n) * vt->cols);
_ttypc_vga_set(vt, vt->vram + (vt->bottom + 1 - n) * vt->cols, vt->attr | ' ', n * vt->cols);
_ttypc_vga_move(vt, vt->top * vt->cols, (vt->top + n) * vt->cols, (vt->bottom - vt->top + 1 - n) * vt->cols);
_ttypc_vga_set(vt, (vt->bottom + 1 - n) * vt->cols, vt->attr | ' ', n * vt->cols);
}
}

Expand All @@ -251,9 +255,9 @@ void _ttypc_vga_rolldown(ttypc_vt_t *vt, unsigned int n)
if (vt->bottom > vt->crow) {
k = min(n, vt->bottom - vt->crow);
l = min(k, vt->scrbsz);
_ttypc_vga_move(vt, vt->vram + (vt->top + k) * vt->cols, vt->vram + vt->top * vt->cols, (vt->bottom - vt->top + 1 - k) * vt->cols);
_ttypc_vga_set(vt, vt->vram + vt->top * vt->cols, vt->attr | ' ', (k - l) * vt->cols);
_ttypc_vga_write(vt, vt->vram + (vt->top + k - l) * vt->cols, vt->scrb + (vt->scrbsz - l) * vt->cols, l * vt->cols);
_ttypc_vga_move(vt, (vt->top + k) * vt->cols, vt->top * vt->cols, (vt->bottom - vt->top + 1 - k) * vt->cols);
_ttypc_vga_set(vt, vt->top * vt->cols, vt->attr | ' ', (k - l) * vt->cols);
_ttypc_vga_write(vt, (vt->top + k - l) * vt->cols, vt->scrb + (vt->scrbsz - l) * vt->cols, l * vt->cols);
vt->scrbsz -= l;
}
}
Expand All @@ -275,15 +279,15 @@ void _ttypc_vga_scroll(ttypc_vt_t *vt, int n)

/* Save scroll origin */
if (!vt->scrbpos)
_ttypc_vga_read(vt->vram, vt->scro, vt->rows * vt->cols);
_ttypc_vga_read(vt, 0, vt->scro, vt->rows * vt->cols);

/* Copy scrollback */
vt->scrbpos += n;
_ttypc_vga_write(vt, vt->vram, vt->scrb + (vt->scrbsz - vt->scrbpos) * vt->cols, min(vt->scrbpos, vt->rows) * vt->cols);
_ttypc_vga_write(vt, 0, vt->scrb + (vt->scrbsz - vt->scrbpos) * vt->cols, min(vt->scrbpos, vt->rows) * vt->cols);

/* Copy scroll origin */
if (vt->scrbpos < vt->rows) {
_ttypc_vga_write(vt, vt->vram + vt->scrbpos * vt->cols, vt->scro, (vt->rows - vt->scrbpos) * vt->cols);
_ttypc_vga_write(vt, vt->scrbpos * vt->cols, vt->scro, (vt->rows - vt->scrbpos) * vt->cols);

if ((vt == vt->ttypc->vt) && vt->cst) {
/* Show cursor */
Expand All @@ -304,7 +308,7 @@ void _ttypc_vga_scrollcancel(ttypc_vt_t *vt)
return;

/* Restore scroll origin */
_ttypc_vga_write(vt, vt->vram, vt->scro, vt->rows * vt->cols);
_ttypc_vga_write(vt, 0, vt->scro, vt->rows * vt->cols);

/* Restore cursor position... */
vt->cpos -= vt->scrbpos * vt->cols;
Expand Down
8 changes: 4 additions & 4 deletions tty/pc-tty/ttypc_vga.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,19 @@ enum {
/* clang-format off */

/* Copies VGA screen buffer to buff */
extern ssize_t _ttypc_vga_read(volatile uint16_t *vga, uint16_t *buff, size_t n);
extern ssize_t _ttypc_vga_read(ttypc_vt_t *vt, size_t offs, uint16_t *buff, size_t n);


/* Copies buff to VGA screen buffer */
extern ssize_t _ttypc_vga_write(ttypc_vt_t* vt, volatile uint16_t *vga, uint16_t *buff, size_t n);
extern ssize_t _ttypc_vga_write(ttypc_vt_t* vt, size_t offs, uint16_t *buff, size_t n);


/* Sets VGA screen buffer characters to val */
extern volatile uint16_t *_ttypc_vga_set(ttypc_vt_t *vt, volatile uint16_t *vga, uint16_t val, size_t n);
extern volatile uint16_t *_ttypc_vga_set(ttypc_vt_t *vt, size_t offs, uint16_t val, size_t n);


/* Moves VGA screen memory */
extern volatile uint16_t *_ttypc_vga_move(ttypc_vt_t *vt, volatile uint16_t *dvga, volatile uint16_t *svga, size_t n);
extern volatile uint16_t *_ttypc_vga_move(ttypc_vt_t *vt, size_t doffs, size_t soffs, size_t n);


/* Switches to another VT */
Expand Down
7 changes: 4 additions & 3 deletions tty/pc-tty/ttypc_vt.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ static void _ttypc_vt_sput(ttypc_vt_t *vt, char c)
switch (vt->escst) {
case ESC_INIT:
/* InseRt Mode */
if (vt->irm)
_ttypc_vga_move(vt, vt->vram + vt->cpos + 1, vt->vram + vt->cpos, vt->cols - vt->ccol - 1);
if (vt->irm) {
_ttypc_vga_move(vt, vt->cpos + 1, vt->cpos, vt->cols - vt->ccol - 1);
}

_ttypc_vt_sdraw(vt, c);

Expand Down Expand Up @@ -787,7 +788,7 @@ int ttypc_vt_init(ttypc_t *ttypc, unsigned int ttybuffsz, ttypc_vt_t *vt)
_ttypc_vtf_str(vt);

/* Clear screen */
_ttypc_vga_set(vt, vt->vram, vt->attr | ' ', vt->cols * vt->rows);
_ttypc_vga_set(vt, 0, vt->attr | ' ', vt->cols * vt->rows);

return EOK;
}
34 changes: 17 additions & 17 deletions tty/pc-tty/ttypc_vtf.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ void _ttypc_vtf_aln(ttypc_vt_t *vt)
vt->cpos = 0;
vt->ccol = 0;
vt->crow = 0;
_ttypc_vga_set(vt, vt->vram, vt->attr | 'E', vt->rows * vt->cols);
_ttypc_vga_set(vt, 0, vt->attr | 'E', vt->rows * vt->cols);
}


Expand Down Expand Up @@ -382,15 +382,15 @@ void _ttypc_vtf_clreos(ttypc_vt_t *vt)
{
switch (vt->parms[0]) {
case 0:
_ttypc_vga_set(vt, vt->vram + vt->cpos, vt->attr | ' ', vt->cols * vt->rows - vt->cpos);
_ttypc_vga_set(vt, vt->cpos, vt->attr | ' ', vt->cols * vt->rows - vt->cpos);
break;

case 1:
_ttypc_vga_set(vt, vt->vram, vt->attr | ' ', vt->cpos + 1);
_ttypc_vga_set(vt, 0, vt->attr | ' ', vt->cpos + 1);
break;

case 2:
_ttypc_vga_set(vt, vt->vram, vt->attr | ' ', vt->cols * vt->rows);
_ttypc_vga_set(vt, 0, vt->attr | ' ', vt->cols * vt->rows);
break;
}
}
Expand All @@ -400,15 +400,15 @@ void _ttypc_vtf_clreol(ttypc_vt_t *vt)
{
switch (vt->parms[0]) {
case 0:
_ttypc_vga_set(vt, vt->vram + vt->cpos, vt->attr | ' ', vt->cols - vt->ccol);
_ttypc_vga_set(vt, vt->cpos, vt->attr | ' ', vt->cols - vt->ccol);
break;

case 1:
_ttypc_vga_set(vt, vt->vram + vt->cpos - vt->ccol, vt->attr | ' ', vt->ccol + 1);
_ttypc_vga_set(vt, vt->cpos - vt->ccol, vt->attr | ' ', vt->ccol + 1);
break;

case 2:
_ttypc_vga_set(vt, vt->vram + vt->cpos - vt->ccol, vt->attr | ' ', vt->cols);
_ttypc_vga_set(vt, vt->cpos - vt->ccol, vt->attr | ' ', vt->cols);
break;
}
}
Expand Down Expand Up @@ -454,8 +454,8 @@ void _ttypc_vtf_il(ttypc_vt_t *vt)
_ttypc_vga_rolldown(vt, p);
}
else {
_ttypc_vga_move(vt, vt->vram + vt->cpos + p * vt->cols, vt->vram + vt->cpos, (vt->bottom - vt->crow + 1 - p) * vt->cols);
_ttypc_vga_set(vt, vt->vram + vt->cpos, vt->attr | ' ', p * vt->cols);
_ttypc_vga_move(vt, vt->cpos + p * vt->cols, vt->cpos, (vt->bottom - vt->crow + 1 - p) * vt->cols);
_ttypc_vga_set(vt, vt->cpos, vt->attr | ' ', p * vt->cols);
}
}
}
Expand All @@ -470,8 +470,8 @@ void _ttypc_vtf_ic(ttypc_vt_t *vt)
else if (p > vt->cols - vt->ccol)
p = vt->cols - vt->ccol;

_ttypc_vga_move(vt, vt->vram + vt->cpos + p, vt->vram + vt->cpos, vt->cols - vt->ccol - p);
_ttypc_vga_set(vt, vt->vram + vt->cpos, vt->attr | ' ', p);
_ttypc_vga_move(vt, vt->cpos + p, vt->cpos, vt->cols - vt->ccol - p);
_ttypc_vga_set(vt, vt->cpos, vt->attr | ' ', p);
}


Expand All @@ -492,8 +492,8 @@ void _ttypc_vtf_dl(ttypc_vt_t *vt)
_ttypc_vga_rollup(vt, p);
}
else {
_ttypc_vga_move(vt, vt->vram + vt->cpos, vt->vram + vt->cpos + p * vt->cols, (vt->bottom - vt->crow + 1 - p) * vt->cols);
_ttypc_vga_set(vt, vt->vram + (vt->bottom + 1 - p) * vt->cols, vt->attr | ' ', p * vt->cols);
_ttypc_vga_move(vt, vt->cpos, vt->cpos + p * vt->cols, (vt->bottom - vt->crow + 1 - p) * vt->cols);
_ttypc_vga_set(vt, (vt->bottom + 1 - p) * vt->cols, vt->attr | ' ', p * vt->cols);
}
}
}
Expand All @@ -508,8 +508,8 @@ void _ttypc_vtf_dch(ttypc_vt_t *vt)
else if (p > vt->cols - vt->ccol)
p = vt->cols - vt->ccol;

_ttypc_vga_move(vt, vt->vram + vt->cpos, vt->vram + vt->cpos + p, vt->cols - vt->ccol - p);
_ttypc_vga_set(vt, vt->vram + vt->cpos + vt->cols - p, vt->attr | ' ', p);
_ttypc_vga_move(vt, vt->cpos, vt->cpos + p, vt->cols - vt->ccol - p);
_ttypc_vga_set(vt, vt->cpos + vt->cols - p, vt->attr | ' ', p);
}


Expand Down Expand Up @@ -569,7 +569,7 @@ void _ttypc_vtf_ris(ttypc_vt_t *vt)
vt->scrbpos = 0;

/* Clear screen */
_ttypc_vga_set(vt, vt->vram, vt->attr | ' ', vt->cols * vt->rows);
_ttypc_vga_set(vt, 0, vt->attr | ' ', vt->cols * vt->rows);
/* Soft reset */
_ttypc_vtf_str(vt);
}
Expand All @@ -585,7 +585,7 @@ void _ttypc_vtf_ech(ttypc_vt_t *vt)
else if (p > vt->cols - vt->ccol)
p = vt->cols - vt->ccol;

_ttypc_vga_set(vt, vt->vram + vt->cpos, vt->attr | ' ', p);
_ttypc_vga_set(vt, vt->cpos, vt->attr | ' ', p);
}


Expand Down

0 comments on commit b7db794

Please sign in to comment.