Wednesday, August 28, 2013

Make Beep on x86 machines

http://hi.baidu.com/sfmewmddznjorye/item/afd09b5b867a9aadacc85757
http://jianggmulab.blogspot.com/search?q=beep

/*
 * IP port: 0x43
 Bits         Usage
 6 and 7      Select channel :
                 0 0 = Channel 0
                 0 1 = Channel 1
                 1 0 = Channel 2
                 1 1 = Read-back command (8254 only)
 4 and 5      Access mode :
                 0 0 = Latch count value command
                 0 1 = Access mode: lobyte only
                 1 0 = Access mode: hibyte only
                 1 1 = Access mode: lobyte/hibyte
 1 to 3       Operating mode :
                 0 0 0 = Mode 0 (interrupt on terminal count)
                 0 0 1 = Mode 1 (hardware re-triggerable one-shot)
                 0 1 0 = Mode 2 (rate generator)
                 0 1 1 = Mode 3 (square wave generator)
                 1 0 0 = Mode 4 (software triggered strobe)
                 1 0 1 = Mode 5 (hardware triggered strobe)
                 1 1 0 = Mode 2 (rate generator, same as 010b)
                 1 1 1 = Mode 3 (square wave generator, same as 011b)
 0            BCD/Binary mode: 0 = 16-bit binary, 1 = four-digit BCD
 *
 */
static void Beep(void)
{
    __asm__ volatile(
// configure Speaker IO controller
// set bit 0, 1 to 1
// IO port: 0x61
    "in $0x61, %al\n"
                "or $0x3, %al\n"
                "out %al, $0x61\n"

// set 8254 timer contol register
// IO port: 0x43
// 0xb6: 1011 0110
// channel2, access mode lo and hi, square wave
                "movb $0xb6, %al\n"
                "out %al, $0x43\n"

// set the high 8 bits of time2 counter
// IO port: 0x42
                "movb $0x0, %al\n"
                "out %al, $0x42\n"

// set the low 8 bits of timer2 counter
// IO port: 0x42
// use value $0x0f to firmware attack, high frequency
// use value $0xff to the configuration attack

                "movb $0x0f, %al\n"
                "out %al, $0x42\n"



   );
}


static void UnBeep(void)
{
    __asm__ volatile(
   // configure speaker IO contorller
   // set bit 0 and 1 to 0;
   // IO port: 0x61
   "in $0x61, %al\n"
   "and $0xFC, %al\n"
   "out %al, $0x61\n"
   );
}

the above code works on any x86 machines. It uses a timer counter existing on all x86 platforms. they all has Programmable interval timer Intel 8253
We use the channal 2

http://wiki.osdev.org/Programmable_Interval_Timer

Thursday, August 22, 2013

redirect stdout and stderr to a file

program [arguments...] 2>&1 | tee -a outfile &

2>&1: directs stderr to stdout

tee receives the content from stdout and write them to a file. -a means append
see man tee for more info.

E.g.,

I want to append the openvz migration log message to a file. This shell script migrates 100 openvz containers in parallel, and wait all of them to finish. It outputs the time at the end.

#!/bin/sh

echo "*********************************************************" | tee -a migrationResult.txt


date | tee -a migrationResult.txt


for i in {1..100}

do
        vzmigrate --live -t  sr1s1 $i 2>&1 | tee -a migrationResult.txt &
done
wait // wait all of the processes to finish
date | tee -a migrationResult.txt

Tuesday, August 20, 2013

time command

root@server102:/etc/init.d# time ./apache2 start
 * Starting web server apache2                                                              [ OK ]

real 0m0.034s
user 0m0.006s
sys 0m0.016s

http://zch051383471952.blogspot.com/2010/01/different-of-real-user-sys-time.html


Real refers to actual elapsed time; User and Sys refer to CPU time used only by the process.
  • Real is wall clock time - time from start to finish of the call. This is all elapsed time including time slices used by other processes and time the process spends blocked (for example if it is waiting for I/O to complete).
  • User is the amount of CPU time spent in user-mode code (outside the kernel) within the process. This is only actual CPU time used in executing the process. Other processes and time the process spends blocked do not count towards this figure.
  • Sys is the amount of CPU time spent in the kernel within the process. This means executing CPU time spent in system calls within the kernel, as opposed to library code, which is still running in user-space. Like 'user', this is only CPU time used by the process.



Monday, August 19, 2013

Access PCI configuration space

Two dedicated I/O ports (to be accessed via IN/OUT instructions):
0xCF8 – selects the address (Bus, Node, Function, Offset)
0xCFC – data port

I/O mapped memory:
PCI configuration space, power management registers, ACPI PM base

Memory mapped:
LAPIC, IOAPIC

More in this presentation:
http://72.3.219.184/conference/2007/papers/rutkowska-joanna-slides.pdf

Tuesday, August 6, 2013

Install httperf on Untuntu

sudo apt-get install httperf

if you get error:
E: Unable to locate package httperf

You need to update your soruce.list

1. generate source list from website:
Generate sources.list : http://debgen.simplylinux.ch/
Don't forget to add contrib and non-free

2. Copy paste source list to /etc/apt/source.list

3. run apt-get update

4. install httperf

More info: 
http://forums.debian.net/viewtopic.php?f=10&t=64694