Nios a solution to the architecture class shell in the soft core CPU

Source: Internet
Author: User
Tags map class
<span id="Label3"></p><p><p>To implement a Class-shell interaction system in nios, users can invoke system functions through commands at the Terminal.<br>Think of Linus when the system function call, in fact, based on the idea is the same, is to check the table, each system function corresponds to an interrupt service number, and then through the 0x80 system call into the kernel, and then look up the table, here you can find the corresponding kernel system functions, review how Linus is Done.<br>Take the system function open as an example</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs r"><span class="hljs-keyword">...</span>){ register int res; va_list arg; va_start(arg,flag); __asm__(<span class="hljs-string">"int $0x80"</span> :<span class="hljs-string">"=a"</span> (res) :<span class="hljs-string">"0"</span> (__NR_open),<span class="hljs-string">"b"</span> (filename),<span class="hljs-string">"c"</span> (flag), <span class="hljs-string">"d"</span><span class="hljs-keyword">if</span> (res>=<span class="hljs-number">0</span>) <span class="hljs-keyword">return</span><span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;}</code></pre></pre><p><p>This open function is a variable parameter function, the study of the variable parameter function in front with a piece of blog has been said, not Much.<br>Mainly looking at these important codes</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs bash"><span class="hljs-string">"int <span class="hljs-variable">$0x80</span>"</span>:<span class="hljs-string">"0"</span> (__NR_open)</code></pre></pre><p><p>This is a software call, __nr_open is an incoming parameter, is a macro definition</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-preprocessor">#<span class="hljs-keyword">define</span> __NR_open 5</span></code></pre></pre><p><p>For this interrupt number with 0x80, you need to find the interrupt vector table with its corresponding interrupt vectors,<br>In the dispatch initialization function sched.c, there are</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs bash"><span class="hljs-keyword">set</span>_system_gate(<span class="hljs-number">0</span>x80,&system_call);</code></pre></pre><p><p>is the interrupt vector corresponding to the interrupt 0x80 is system_call, so this is why everyone called 0x80 is the system function call interrupt Number.</p></p><p><p>Look at Systen_call.</p></p><pre class="prettyprint"><code class=" hljs perl">_system_call:<span class="hljs-keyword"><span class="hljs-keyword">Push</span></span> <span class="hljs-variable"><span class="hljs-variable">%ds</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Push</span></span> <span class="hljs-variable"><span class="hljs-variable">%es</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Push</span></span> <span class="hljs-variable"><span class="hljs-variable">%fs</span></span>Pushl<span class="hljs-variable"><span class="hljs-variable">%eax</span></span> <span class="hljs-comment"><span class="hljs-comment"># Save the Orig_eax</span></span>Pushl<span class="hljs-variable"><span class="hljs-variable">%edx</span></span>Pushl<span class="hljs-variable"><span class="hljs-variable">%ecx</span></span> <span class="hljs-comment"><span class="hljs-comment"># Push%ebx,%ecx,%edx as Parameters</span></span>Pushl<span class="hljs-variable"><span class="hljs-variable">%EBX</span></span> <span class="hljs-comment"><span class="hljs-comment">#</span> to The system call</span>Movl<span class="hljs-variable"><span class="hljs-variable"></span> $</span>X1<span class="hljs-number"><span class="hljs-number">0</span></span>,<span class="hljs-variable"><span class="hljs-variable">%edx</span></span> <span class="hljs-comment"><span class="hljs-comment"># Set up ds,es to kernel space</span></span>mov<span class="hljs-variable"><span class="hljs-variable">%DX</span></span>,<span class="hljs-variable"><span class="hljs-variable">%ds</span></span>mov<span class="hljs-variable"><span class="hljs-variable">%DX</span></span>,<span class="hljs-variable"><span class="hljs-variable">%es</span></span>Movl<span class="hljs-variable"><span class="hljs-variable"></span> $</span>x17,<span class="hljs-variable"><span class="hljs-variable">%edx</span></span> <span class="hljs-comment"><span class="hljs-comment"># FS points to local data space</span></span>mov<span class="hljs-variable"><span class="hljs-variable">%DX</span></span>,<span class="hljs-variable"><span class="hljs-variable">%fs</span></span>Cmpl _nr_syscalls,<span class="hljs-variable"><span class="hljs-variable">%eax</span></span>Jae Bad_sys_call call _sys_call_table (,<span class="hljs-variable"><span class="hljs-variable">%eax</span></span>,<span class="hljs-number"><span class="hljs-number">4</span></span>) PUSHL<span class="hljs-variable"><span class="hljs-variable">%eax</span></span></code></pre><p><p>Look at the important lines of code</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs perl">cmpl _NR_syscalls,<span class="hljs-variable">%eax</span> call _sys_call_table(,<span class="hljs-variable">%eax</span>,<span class="hljs-number">4</span></code></pre></pre><p><p>The first sentence compares the arguments passed in to determine what system functions need to be called<br>The second sentence calls the corresponding system function</p></p><pre class="prettyprint"><code class=" hljs bash">Fn_ptr sys_call_table[] = {sys_setup, Sys_<span class="hljs-keyword"><span class="hljs-keyword">Exit</span></span>, sys_fork, Sys_<span class="hljs-built_in"><span class="hljs-built_in">Read</span></span>, sys_write, sys_open, sys_close, sys_waitpid, sys_creat, sys_link,sys_unlink, sys_execve, sys_chdir, sys_time, sys_ mknod, sys_chmod,sys_chown, Sys_<span class="hljs-keyword"><span class="hljs-keyword"></span> break</span>, sys_stat, sys_lseek, sys_getpid, sys_mount,sys_umount, sys_setuid, sys_getuid, sys_stime, sys_ptrace, sys_alarm,sys_ fstat, sys_pause, sys_utime, sys_stty, sys_gtty, sys_access,sys_nice, sys_ftime, sys_sync, sys_kill, sys_rename, sys_ mkdir,sys_rmdir, sys_dup, sys_pipe, sys_times, sys_prof, sys_brk, sys_setgid,sys_getgid, sys_signal, sys_geteuid, sys_ getegid, sys_acct, sys_phys,sys_lock, sys_ioctl, sys_fcntl, sys_mpx, sys_setpgid, sys_ulimit,sys_uname, sys_umask, sys_ chroot, sys_ustat, sys_dup2, sys_getppid,sys_getpgrp, sys_setsid, sys_sigaction, sys_sgetmask, sys_ssetmask,sys_ setreuid,sys_setregid, sys_sigsuspend, sys_sigpending, sys_sethostname,sys_setrlimit, sys_getrlimit, sys_getrusage, sys_gettimeofday, sys_settimeofday, sys_getgroups, sys_setgroups, sys_select, sys_symlink,sys_lstat, sys_readlink, sys_uselib};</code></pre><p><p>This is actually an array, where <code>typedef int (*fn_ptr)();</code><br>So is the function pointer array, Interrupt service number as the index of the array to look up the table,<br>It is known that Open's service number is 5, then sys_call_table[5] does correspond to the address of the function Sys_open.<br>So this completes the whole of a call from the user to the kernel,<br></p></p><p><p>So in Nois to develop a class shell interaction system, It can be borrowed from this model, in this model to simplify, here the most important is that table (function pointer array), in the Nios is a single-threaded run, so interrupt this block can be removed, using the way of query to Complete. There is how to match the user input commands and system commands, whether or not want to Linus the same number to correspond, but the actual user input is a string, so whether the byte is a string match, since it is the string can not be used in the way the array subscript, think of C + + map class This is an associative variable that can be written as a struct, or it can achieve this effect.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs d"><span class="hljs-keyword">typedef</span><span class="hljs-keyword">struct</span> key{ 用户命令字符串; 用户参数; 命令处理函数;}KEY;</code></pre></pre><p><p>This is actually a command of all the relevant things, and then using arrays to store these command structures.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs css"><span class="hljs-tag">KEY</span> <span class="hljs-tag">cmd_table</span><span class="hljs-attr_selector">[]</span>;</code></pre></pre><p><p>By comparing the first member of each struct to determine the command and then invoke the command Function.<br><br>The final implementation of the initialization cmd_table is as follows<br></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Nios a solution to the architecture class shell in the soft core CPU</p></p></span>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.