source: trunk/kernel/syscalls/sys_get_best_core.c @ 639

Last change on this file since 639 was 639, checked in by alain, 2 years ago

ntroduce three new almos-mkh specific syscalls.

File size: 4.3 KB
Line 
1/*
2 * sys_get_best_core.c - Kernel function implementing the "get_best_core" syscall.
3 *
4 * Author    Alain Greiner (2016,2017,2018,2019)
5 * 
6 * Copyright (c) UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2.0 of the License.
13 *
14 * ALMOS-MKH is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <hal_kernel_types.h>
25#include <hal_uspace.h>
26#include <hal_vmm.h>
27#include <hal_special.h>
28#include <errno.h>
29#include <core.h>
30#include <thread.h>
31#include <process.h>
32#include <vmm.h>
33#include <printk.h>
34
35#include <syscalls.h>
36
37///////////////////////////////////////////
38int sys_get_best_core( uint32_t   base_cxy,
39                       uint32_t   level,
40                       uint32_t * cxy,
41                       uint32_t * lid )
42{
43    int       ret;
44    vseg_t  * vseg;
45    uint32_t  k_cxy;
46    uint32_t  k_lid;
47
48    thread_t  * this    = CURRENT_THREAD;
49    process_t * process = this->process;
50
51#if (DEBUG_SYS_GET_BEST_CORE || CONFIG_INSTRUMENTATION_SYSCALLS)
52uint64_t     tm_start = hal_get_cycles();
53#endif
54
55#if DEBUG_SYS_GET_BEST_CORE
56if( DEBUG_SYS_GET_BEST_CORE < tm_start )
57printk("\n[%s] thread[%x,%x] enter / base_cxy %x / level %d / cycle %d\n",
58__FUNCTION__, process->pid, this->trdid, base_cxy, level, (uint32_t)tm_start );
59#endif
60
61   // check <cxy> buffer in user space
62    if( vmm_get_vseg( process , (intptr_t)cxy , &vseg ) )
63        {
64
65#if DEBUG_SYSCALLS_ERROR
66printk("\n[ERROR] in %s : cxy buffer %x unmapped / thread %x / process %x\n",
67__FUNCTION__ , (intptr_t)cxy , this->trdid , process->pid );
68#endif
69        this->errno = EINVAL;
70                return -1;
71        }
72
73    // check <lid> buffer in user space
74    if( vmm_get_vseg( process , (intptr_t)lid , &vseg ) )
75        {
76
77#if DEBUG_SYSCALLS_ERROR
78printk("\n[ERROR] in %s : lid buffer unmapped %x / thread %x / process %x\n",
79__FUNCTION__ , (intptr_t)lid , this->trdid , process->pid );
80#endif
81        this->errno = EINVAL;
82                return -1;
83        }
84
85    // check level argument
86        if( level > 5 ) 
87        {
88
89#if DEBUG_SYSCALLS_ERROR
90printk("\n[ERROR] in %s : level argument %d too large / thread %x / process %x\n",
91__FUNCTION__ , level , this->trdid , process->pid );
92#endif
93        this->errno = EINVAL;
94                return -1;
95        }
96
97    // get extended pointer on the macro-cluster root node
98    xptr_t root_xp = dqdt_get_root( base_cxy , level );
99
100    if( root_xp == XPTR_NULL )  // macro-cluster undefined
101    {
102
103#if DEBUG_SYS_GET_BEST_CORE
104printk("\n[%s] thread[%x,%x] do nothing : macro cluster undefined\n",
105__FUNCTION__, process->pid, this->trdid );
106#endif
107        k_lid = -1;
108        k_cxy = -1;
109        ret   = 1;
110    }
111    else                        // macro-cluster exist
112    {
113            k_cxy = (uint32_t)dqdt_get_cluster_for_thread( root_xp );
114            k_lid = (uint32_t)cluster_select_local_core( k_cxy );
115        ret   = 0;
116
117#if DEBUG_SYS_GET_BEST_CORE
118printk("\n[%s] thread[%x,%x] succeed : root_cxy %x / best_cluster %x / best_lid %d\n",
119__FUNCTION__, process->pid, this->trdid, GET_CXY( root_xp ), k_cxy, k_lid );
120#endif
121
122    }
123
124    // copy to user space
125        hal_copy_to_uspace( cxy,
126                        XPTR( local_cxy , &k_cxy ),
127                        sizeof(uint32_t) );
128
129        hal_copy_to_uspace( lid,
130                        XPTR( local_cxy , &k_lid ),
131                        sizeof(uint32_t) );
132
133#if (DEBUG_SYS_GET_BEST_CORE || CONFIG_INSTRUMENTATION_SYSCALLS)
134uint64_t     tm_end = hal_get_cycles();
135#endif
136
137#if DEBUG_SYS_GET_BEST_CORE
138if( DEBUG_SYS_GET_BEST_CORE < tm_end )
139printk("\n[%s] thread[%x,%x] exit / cycle %d\n",
140__FUNCTION__ , process->pid, this->trdid, (uint32_t)tm_end );
141#endif
142
143#if CONFIG_INSTRUMENTATION_SYSCALLS
144hal_atomic_add( &syscalls_cumul_cost[SYS_GET_BEST_CORE] , tm_end - tm_start );
145hal_atomic_add( &syscalls_occurences[SYS_GET_BEST_CORE] , 1 );
146#endif
147
148        return ret; 
149
150}  // end sys_get_best_core()
Note: See TracBrowser for help on using the repository browser.