Commit 84708de2 authored by Jithu Joseph's avatar Jithu Joseph Committed by Vikram Narayanan
Browse files

Standalone utility functions to be used inside guest

parent 101d7032
//
// tesla.c
//
//
// Created by jithu joseph on 2/28/14.
// some standalone utility routines to be used by guest
// compile using -static -fno-builtin
//
//#include <stdio.h>
typedef unsigned int uint;
unsigned long
strlen(const char *s)
{
int n;
for(n = 0; s[n]; n++)
;
return n;
}
static int lcd_puts(const char *buf)
{
long ret;
asm volatile("movq $1, %%rax \n\t" // SYS_write
"movq $1, %%rdi \n\t" // STDOUT
"movq %1, %%rsi \n\t" // string
"movq %2, %%rdx \n\t" // string len
"vmcall \n\t"
"movq %%rax, %0 \n\t" :
"=r" (ret) : "r" (buf), "r" (strlen(buf)) :
"rax", "rdi", "rsi", "rdx");
return ret;
}
static int putc(int fd, const char c)
{
long ret;
asm volatile("movq $1, %%rax \n\t" // SYS_write
"movq $1, %%rdi \n\t" // STDOUT
"movq %1, %%rsi \n\t" // string
"movq $1, %%rdx \n\t" // string len
"vmcall \n\t"
"movq %%rax, %0 \n\t" :
"=r" (ret) : "r" (&c) :
"rax", "rdi", "rsi", "rdx");
return ret;
}
static void
printint(int fd, int xx, int base, int sgn)
{
static char digits[] = "0123456789ABCDEF";
char buf[16];
int i, neg;
uint x;
neg = 0;
if(sgn && xx < 0){
neg = 1;
x = -xx;
} else {
x = xx;
}
i = 0;
do{
buf[i++] = digits[x % base];
}while((x /= base) != 0);
if(neg)
buf[i++] = '-';
while(--i >= 0)
putc(fd, buf[i]);
}
// Print to the given fd. Only understands %d, %x, %p, %s.
void
printf(int fd, char *fmt, ...)
{
char *s;
int c, i, state;
uint *ap;
state = 0;
ap = (uint*)(void*)&fmt + 1;
for(i = 0; fmt[i]; i++){
c = fmt[i] & 0xff;
if(state == 0){
if(c == '%'){
state = '%';
} else {
putc(fd, c);
}
} else if(state == '%'){
if(c == 'd'){
printint(fd, *ap, 10, 1);
ap++;
} else if(c == 'x' || c == 'p'){
printint(fd, *ap, 16, 0);
ap++;
} else if(c == 's'){
s = (char*)*ap;
ap++;
if(s == 0)
s = "(null)";
while(*s != 0){
putc(fd, *s);
s++;
}
} else if(c == 'c'){
putc(fd, *ap);
ap++;
} else if(c == '%'){
putc(fd, c);
} else {
// Unknown % sequence. Print it to draw attention.
putc(fd, '%');
putc(fd, c);
}
state = 0;
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment